如何从http请求中获取文本

时间:2015-10-24 23:39:58

标签: javascript node.js api http

如何从我构建的http api中获取文本?所以我构建了一个像这样http://IPADDRESS/getText/hkjfskhfkjdshfkj的http api,它响应了#34; HI"或类似的东西,所以我的问题是如何调用api然后使用javascript将其读入我的网页上的变量?

服务器也是用nodejs编写的。

2 个答案:

答案 0 :(得分:0)

您可以提供Rest服务,或只是制作一个regular expression,可以从window.location.href

中过滤您想要的部分。

答案 1 :(得分:0)

您可以使用ajax请求执行此操作。

jQuery示例:

var someVariable = ""; // variable is empty at start

$.ajax({
             type: 'GET',
             url:  'http://IPADDRESS/getText/hkjfskhfkjdshfkj'
      }).done( function (responseText) {
                // responseText contains 'HI'or something like that
                someVariable = responseText;
                alert(someVariable);
      });
相关问题