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

标签 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&jquery ajax中进行https调用

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

最佳答案

虽然我确信可以使用 jQuery 和 jsDom 来完成此任务,但还有更多经过验证的真实库可以实现此目的。例如,许多 Node 开发人员使用 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
});

关于Nodejs 中的 jquery ajax 无法使用 https 获取/发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34183739/

相关文章:

javascript - 更新模型的属性

node.js - 为什么我的 Heroku Express API 数据是持久的,即使它仅来自变量

php - 如何使用explode()将2个单词分成2个不同的字符串

jquery - 为什么 Ajax 脚本无法在 IIS 7.5 Win 2008 R2 服务器上运行?

javascript - .focus() 与 Zepto 不工作

javascript - 如何向 ajax 文件上传添加额外的 POST 参数?

javascript - 如何从单独的 php 文件 onClick 执行 php 函数

node.js - Node.js 在 SharePoint 上有什么用途?

javascript - JS 无法在同一对象中本地化函数

javascript - 让某些 JS 代码等到另一个命令完成后再执行