Ajax请求失败


                    

发布请求保持待处理状态。
我想记录传入请求的JSON正文。
使用Postman时该请求有效,但AJAX请求不起作用</ p>

节点代码:</ p>

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

var hostname ='localhost';
var port = 3000;

var app = express();

app.use(morgan('dev'));

var qRouter = express.Router();
qRouter.use(bodyParser.json());

qRouter.route('/')
.all(function(req,res,next){
       res.header('Access-Control-Allow-Origin',“ *”)
       res.writeHead(200,{'Content-Type':'application / json'});
       下一个();
})
.post(function(req,res){
   console.log(要求正文);
   res.end(“ lalal”);
})
app.use('/ play',qRouter);

app.listen(端口,主机名,函数(){
console.log(服务器在http:// $ {hostname}:$ {port} /上运行);
});
</ code> </ pre>

AJAX请求</ p>

  var d = {
     “评分”:5
     “ comment”:“送任何人到天堂,希望我可以让我婆婆吃!”,
         “作者”:“ Paul McVites”
   }

   $ .ajax({
       网址:“ http:// localhost:3000 / play”,
       输入:“ POST”,
       contentType:“ application / json”,
       数据:JSON.stringify(d)
   });
</ code> </ pre>
     </ div>

展开原文

原文

The Post requests stay pending. I wanted to log the JSON body of the incoming request. The request works when using Postman but the AJAX requests dont work

Node Code:

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

var hostname = 'localhost';
var port = 3000;

var app = express();

app.use(morgan('dev'));

var qRouter = express.Router();
qRouter.use(bodyParser.json());

qRouter.route('/')
.all(function(req,res,next) {
      res.header('Access-Control-Allow-Origin', "*")
      res.writeHead(200, { 'Content-Type': 'application/json' });
      next();
})
.post(function(req,res){
  console.log(req.body);
  res.end("lalal");
})
app.use('/play',qRouter);

app.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});

AJAX REQUEST

    var d = {
    "rating": 5,
    "comment": "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
        "author": "Paul McVites"
  }

  $.ajax({
      url: 'http://localhost:3000/play',
      type: 'POST',
      contentType: 'application/json',
      data: JSON.stringify(d)
  });

1个回答


我认为您没有使用jQuery处理您的ajax调用响应。 您正在发送数据,但是我看不到您收到服务器响应。 POSTMAN会自动为您处理。</ p>
     </ div>

展开原文

原文

I think you are not handling your the response of your ajax call with jQuery. You are sending the data, but I don't see you receiving the server response. POSTMAN handles this automatically for you.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐