没有通过jquery发布请求

post request with jquery not going through

本文关键字:请求 布请求 jquery      更新时间:2023-10-16

所以我有下面的jquert代码

$.ajax({
        url: '/game',
        type: 'post',
        dataType: 'json',
        success: function (data) {
           if (data.status != "ok") {
               console.log(data);
               alert(data);
           }
        },
        data: {game_id : 1, game_stage : 1}
});

或者即使我把它改成这个

$.post("/game", {game_id:1, game_stage:1}, function(data) {
   if (data.status != "ok") {
       console.log(data);
       alert(data);
   }
}, 'json');

我有一个C++服务器来调试它,它只输出发送过来的原始HTTP请求,出于某种原因,我总是得到

POST /game HTTP/1.1
Host: localhost:5000
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Origin: http://localhost:5000
Content-Length: 22
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4
Referer: http://localhost:5000/
DNT: 1

为什么JSON对象没有被传递到请求中?类似的GET请求也可以正常工作。

这是我第一次使用JavaScript,所以请原谅我的愚蠢错误

尝试更改

url: '/game',

url: 'game',

我假设"game"在当前目录中,没有扩展名。

试着这样使用:

$.ajax({
url: '/game',
type:"POST",
data: {game_id : 1, game_stage : 1} ,
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(data){
  if (data.status != "ok") {
   console.log(data);
   alert(data);
}
}
});