![](/img/trans.png)
[英]Why does my jQuery Ajax GET-request not receive a response from the server?
[英]Why does the server not receive the ajax request?
我必須使用集成測試來測試一個網站。 該網站將所有請求發送到代理:
var express = require("express"),
http = require("http"),
port = (process.env.PORT || 8001),
server = module.exports = express(),
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
// SERVER CONFIGURATION
// ====================
server.configure(function() {
server.use(function(req, res, next) {
console.log(req.url);
if (req.url.indexOf('/any/thing') === 0) {
console.log("url: " + req.url + ",\nheaders: " + req.headers + ",\nmethod: " + req.method);
proxy.web(req, res, {target: 'http://127.0.0.1:1337'});
} else {
//console.log("url: " + req.url + ",\nheaders: " + req.headers + ",\nmethod: " + req.method);
next();
}
});
server.use('/anything', express["static"](__dirname + "/../public"));
server.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
server.use(express.bodyParser());
server.use(server.router);
});
// SERVER
// ======
// Start Node.js Server
http.createServer(server).listen(port);
該代理應與此服務器通信:
var http = require('http');
http.createServer(function (req, res) {
console.log("url: " + req.url + ",\nheaders: " + req.headers + ",\nmethod: " + req.method);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("{'user': 'garrarufa', 'loginToken': 'token'}");
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
使用ajax登錄:
$.ajax(this.apipath + "/login/", {
type: "GET",
async: false,
success: function(...){...},
error: function(...){...}
});
當我運行登錄請求時,立即調用error
。 該請求不會出現在代理服務器上。 至少代理不將任何內容打印到控制台。 這里發生了什么? 不應將ajax請求發送到服務器嗎?
您的代理正在運行
port = (process.env.PORT || 8001),
您的客戶代碼發送到哪個端口? 即this.apipath設置為什么?
$.ajax(this.apipath + "/login/", {
type: "GET",
async: false,
success: function(...){...},
error: function(...){...}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.