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/