2013-07-29 126 views
0

收到$ _ POST变量我在做,看起来一个Ajax请求喜欢从jQuery的Ajax请求

var object = JSON.stringify(object); 
// var url = "http://"+baseURL +"/"+endpoint; 

$.ajax({ 
    contentType: "application/json", 
    dataType: 'json', 
    type:type, 
    data:object, 
    url:endpoint, 
    success:function(data){ 
     if (typeof callback == "function"){ 
      alert(data); 
     } 
    }, 

    error: function (xhr, textStatus, errorThrown) { 
     console.log(xhr.statusText); 
     console.log(xhr.responseText); 
     console.log(xhr.status); 
     console.log(errorThrown); 
    } 
}); 

其中var=object是一个字符串化JSON objectby这使得它成为Ajax请求的时间。在PHP端,我试图做

<?php 
    echo ($_POST['object']); 
    exit; 
?> 

赶上变量和我的成功回调函数提醒数据,“空”。我究竟做错了什么?

感谢, 亚历克斯

+1

这不是POST。 –

+0

“type”的确切位置在哪里?如果这不是'POST',那么你不会做一个职位... –

+0

print_r($ _ POST); –

回答

1

跳过json.stringify你不想要的数据,在后机身JSON文本。为了填充帖子数组,它需要被发送为application/x-www-form-urlencoded。要在jquery中做到这一点,只需将数据属性设置为对象而不是字符串。

// remove this.... var object = JSON.stringify(object); 
// var url = "http://"+baseURL +"/"+endpoint; 

$.ajax({ 
    dataType: 'json', 
    type:"POST", // <--- Should be post 
    data:object, 
    url:endpoint, 
    success:function(data){ 
     if (typeof callback == "function"){ 
      alert(data); 
     } 
    }, 

    error: function (xhr, textStatus, errorThrown) { 
     console.log(xhr.statusText); 
     console.log(xhr.responseText); 
     console.log(xhr.status); 
     console.log(errorThrown); 
    } 
}); 

可以获取当前发送数据的数据,但您必须在PHP端进行一些更多的工作。

$_POST = json_decode(file_get_contents("php://input"),true); 
+0

即时通讯对不起,我需要编辑我的conetntType吗? –

+1

你可以忽略它,或者将它设置为'application/x-www-form-urlencoded'(如果你没有设置contenttype,这就是发送的内容) – Orangepill