简体   繁体   English

为什么服务器未收到Ajax请求?

[英]Why does the server not receive the ajax request?

I have to test a website with integration tests. 我必须使用集成测试来测试一个网站。 The website sends all requests to a proxy: 该网站将所有请求发送到代理:

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);

This proxy should communicate with this server: 该代理应与此服务器通信:

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/');

To login ajax is used: 使用ajax登录:

$.ajax(this.apipath + "/login/", {
    type: "GET",
    async: false,
    success: function(...){...},
    error: function(...){...}
});

When I run the login request, immediately error is called. 当我运行登录请求时,立即调用error The request does not appear at the proxy server. 该请求不会出现在代理服务器上。 At least the proxy does not print anything to the console. 至少代理不将任何内容打印到控制台。 What is going on here? 这里发生了什么? Shouldn't the ajax request be sent to the server? 不应将ajax请求发送到服务器吗?

Your proxy is running on 您的代理正在运行

port = (process.env.PORT || 8001),

What port is your client code sending to? 您的客户代码发送到哪个端口? Ie what is this.apipath set to? 即this.apipath设置为什么?

$.ajax(this.apipath + "/login/", {
    type: "GET",
    async: false,
    success: function(...){...},
    error: function(...){...}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM