简体   繁体   English

Ajax到node.js Express模块​​未给出响应

[英]Ajax to node.js express module isn't giving a response

I'm using node.js and JQuery to communicate. 我正在使用node.js和JQuery进行通信。 Whenever I send a post request to my node.js express server, it receives the request but does not seem to give a response. 每当我将发帖请求发送到我的node.js express服务器时,它都会收到请求,但似乎没有给出响应。

Client code: 客户代码:

     $("#submitdetails").click(function(){
  $.post("http://neztorian.xyz:26/",
    {
        name: "Donald Duck",
        city: "Duckburg"
    },
    function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });

});

Sever code: 服务器代码:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
console.log("Running");
app.post('/', function (req, res) {
console.log(req.body);

res.send("recieved");

})
app.listen(26);

The server receives the post request and logs it to console but the alert message does not appear on the client. 服务器收到发布请求,并将其记录到控制台,但警报消息未出现在客户端上。 Thank you for your help. 谢谢您的帮助。 Sorry if this is a stupid question. 抱歉,这是一个愚蠢的问题。

Edit: Thank you for your answers. 编辑:谢谢您的回答。 Problem was: Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://neztorian.xyz:26' is therefore not allowed access. 问题是: Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://neztorian.xyz:26' is therefore not allowed access. Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://neztorian.xyz:26' is therefore not allowed access. I've fixed the issue by adding that header to the server. 我已通过将该标头添加到服务器来解决此问题。

The request is not allowed by the browser because the server is not sending the Access-Control-Allow-Origin header. 浏览器不允许该请求,因为服务器未发送Access-Control-Allow-Origin标头。

Set the header and request are allowed. 设置标头和请求是允许的。

app.post('/', function (req, res) {
  console.log(req.body);
  res.header('Access-Control-Allow-Origin', 'http://neztorian.xyz:26'); 
  res.send("recieved");
});

Read more here: CORS 在这里阅读更多: CORS

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

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