簡體   English   中英

Ajax到node.js Express模塊​​未給出響應

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

我正在使用node.js和JQuery進行通信。 每當我將發帖請求發送到我的node.js express服務器時,它都會收到請求,但似乎沒有給出響應。

客戶代碼:

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

});

服務器代碼:

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

服務器收到發布請求,並將其記錄到控制台,但警報消息未出現在客戶端上。 謝謝您的幫助。 抱歉,這是一個愚蠢的問題。

編輯:謝謝您的回答。 問題是: 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標頭。

設置標頭和請求是允許的。

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

在這里閱讀更多: CORS

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM