nodejs中的jquery ajax无法使用https获取/发布

时间:2015-12-09 16:19:14

标签: jquery ajax node.js https

    var app, express, fs, jquery, jsdom;

    jsdom = require('jsdom');

    fs = require('fs');

    express = require('express');

    jquery = fs.readFileSync('./jquery-2.0.3.min.js').toString();

    jsdom.env({
      html: '<html></html>',
      src: [jquery],
      done: function(errors, window) {
        var $;
        $ = window.$;
        window.print = console.log;
        console.log(errors);
        return $.ajax({
          url: "https://www.baidu.com",
          type: "GET",
          error: function(jqXHR, textStatus, errorThrown) {
            return print(jqXHR.url + " " + jqXHR.status + " " + " " + textStatus + " " + errorThrown);
          },
          success: function(data, textStatus, jqXHR) {
            return console.log(data);
          }
        });
      }
    });

    app = express();

    app.listen('3000', function() {});

我要将项目迁移到nodejs。 它使用jquery ajax来传递https后端。

上面的演示响应是:

location.replace(location.href.replace("https://","http://"));

如何在nodejs&amp; jquery ajax

中进行https调用

ps:如果我使用nodejs + jquery ajax,我无法从fiddler获取任何包

1 个答案:

答案 0 :(得分:0)

虽然我确信可以使用jQuery和jsDom来实现这一目标,但是还有更多尝试过的真实库。例如,许多节点开发人员使用requestjs library

所以而不是

$.ajax({
      url: "https://www.baidu.com",
      type: "GET",
      error: function(jqXHR, textStatus, errorThrown) {
        return print(jqXHR.url + " " + jqXHR.status + " " + " " + textStatus + " " + errorThrown);
      },
      success: function(data, textStatus, jqXHR) {
        return console.log(data);
      }
    });

你会这样做:

request(
    uri: "https://www.baidu.com",
    method: "GET",
    function(err, res, body){
        // handle the err and do what you want with the response
});