请求jquery不通过

时间:2016-04-09 04:24:33

标签: javascript jquery c++ ajax http

所以我有以下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,原谅我的愚蠢错误

2 个答案:

答案 0 :(得分:0)

尝试更改

url: '/game',

url: 'game',

我假设'游戏'在当前目录中没有扩展名。

答案 1 :(得分:0)

尝试使用这样:

$.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);
}
}
});
相关问题