2016-08-13 160 views
0

我有JSON的数组:为什么我的PHP后端没有JSON后的数据?

[Object { path="/usr/share/htvcenter/storage/Windows", imgid="14698227485587"}, Object { path="/usr/share/htvcenter/storage/WindowsServer", imgid="14701636866762"}] 

我送这个阵列使用Ajax:

$.ajax({ 
      type: 'POST', 
      url: urlstring, 
      contentType: "application/json", 
      data: JSON.stringify(parameters), 

      success: function(data){ 
       $('.lead').hide(); 
       blackalert('Removed successfully!'); 
      } 
    }); 

我看到这个萤火对邮寄: enter image description here

但服务器的答案是with empty $ _POST: enter image description here

服务器的代码i s:

if (isset($_GET['treeaction']) && $_GET['treeaction'] == 'remove') { 
     echo 'here'; 
     var_dump($_POST); die(); 
} 

我在做什么错了?

+0

你想获得'$ _POST',但在你的代码中它是'$ _GET'这是哪个? – guradio

+0

的var_dump($ _ POST);所以主要数据在后。进去我检查删除的动作,得到进去的网址,在其POST发送 –

回答

0

当你发送一个POST请求你不需要将内容类型设置为application/json。通过这样做,这是不再是一个普通POST请求(与表单值),但有效载荷的要求。

从您的服务器上的负载要求得到这些数据,您可以使用:

$content = file_get_contents("php://input"); 
var_dump(json_decode($content)); // In case the content is a json string. 

如果您想发送一个普通的请求,你可以使用:

$.ajax({ 
     type: 'POST', 
     .... 
     data: {'data' : parameters}, 
     success: function(data){ 
     alert(data); 
     } 
}); 

没有contentType和没有JSON.stringify
这样你可以得到的数据与$_POST['data']

+0

谢谢你这么多 –

+0

欢迎您:) – Dekel

+0

此外还将欣赏voteup – Dekel

相关问题