jquery - 让 Node.js 中的 http.request 适用于浏览器

标签 jquery ajax node.js http cross-browser

我想编写一个可以与 Node.js浏览器 一起使用的代码库插件。我正在 Node.js 中编写基本代码,然后使用 Node 模块 browserify 将其转换为支持浏览器版本。当我在 Node.js 中执行代码时,我的所有 GETPOST 请求都工作正常。如果我编写 ajax 请求,它对于浏览器来说效果很好。我正在使用 http Node 模块向远程服务器发出请求。如何使远程服务器请求在浏览器版本中工作,而无需编写任何单独的 ajax 请求?主要思想是保持相同的代码库并使其以两种方式工作。 我有以下用 Node.js 编写的示例代码

var http = require('http');

function Test() {
    var self = this;
}

Test.prototype.get_data = function() {
    var fetch = http.request("http://stackoverflow.com/", function(res) {
        var result = ""
        res.on('data', function(chunk) {
            result += chunk;
        });
        res.on('data', function(chunk) {
            console.log("response.................");
            console.log(result);
        });
    });
    fetch.on('error', function(e) {
        console.log("error..........");
        console.log(e);
    });
    fetch.end();
    return 'hello world'
};

API = new Test();

module.exports = API;

现在,当我通过调用 API.get_data() 执行 Node.js 中的代码时,它对我来说工作得很好。但是,在我使用 browserify 转换代码,然后通过 API.get_data()HTML 文件调用后,出现以下错误 XMLHttpRequest 无法加载 http://stackoverflow.com/。当我在 Chrome 中运行时,Access-Control-Allow-Origin 不允许 Origin http://localhost。 。知道如何在不为浏览器编写 jsonp ajax 的情况下使相同的 Node.js 代码库工作。 下面提到的是我在 Mozilla 中运行时的 header 。

Response Headers
Cache-Control   public, max-age=21
Content-Encoding    gzip
Content-Length  36325
Content-Type    text/html; charset=utf-8
Date    Tue, 27 May 2014 20:03:47 GMT
Expires Tue, 27 May 2014 20:04:10 GMT
Last-Modified   Tue, 27 May 2014 20:03:10 GMT
Vary    *
X-Frame-Options SAMEORIGIN

Request Headers
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive
Cookie  __qca=P0-1262336656-1387719953106; __utma=140029553.331521903.1387719953.1389033371.1389035323.13; __utmz=140029553.1389035323.13.13.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); prov=3d930d0c-1785-4421-b319-33036243183e; _ga=GA1.2.331521903.1387719953
Host    stackoverflow.com
Origin  http://127.0.0.1
Referer http://127.0.0.1/index.html
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0

2 requests

51.2 KB

最佳答案

browserify 已经有 http module for the browser ,但您无需执行任何特殊操作即可获得它。当您浏览使用 require('http')

的脚本时,浏览器会自动使用它。

尝试解决 CORS 错误问题:

var fetch = http.request({
  host: "stackoverflow.com",
  port: 80,
  path: "/",
  method: "GET",
  withCredentials: false // this is the important part
}, function(res) {
    var result = ""
    res.on('data', function(chunk) {
        result += chunk;
    });
    res.on('end', function() {
        console.log("response.................");
        console.log(result);
    });
});

关于jquery - 让 Node.js 中的 http.request 适用于浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23896619/

相关文章:

javascript - IE 中的 Bootstrap 输入验证

javascript - jQuery ajax 请求仅在第二次调用时返回值

javascript - Ajax uploader 错误500

javascript - 如何用css和jquery改变表格点击行的文字颜色

javascript - 使用多个 Deferred 和 Promises - jquery

ajax - 无法使用动态 JSON

javascript - 使用 npm 发布 es6 和 es5 源代码

node.js - 是否有类似角度注入(inject)器的nod​​ejs依赖注入(inject)解决方案

node.js - 如何在 TypeScript 中正确要求和声明 Node 库类型?

javascript - 带搜索选项的选择框