【问题标题】:jquery ajax in nodejs can not get/post with httpsnodejs中的jquery ajax无法使用https获取/发布
【发布时间】:2016-08-16 08:28:12
【问题描述】:
    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&jquery ajax 中进行 https 调用

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

【问题讨论】:

    标签: jquery ajax node.js https


    【解决方案1】:

    虽然我确信可以使用 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
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 2020-05-13
      • 2023-04-02
      • 1970-01-01
      • 2012-11-23
      • 2021-08-23
      相关资源
      最近更新 更多