简体   繁体   English

发送json数据到服务器jquery.ajax

[英]send json data to server jquery.ajax

Another json related question unfortunately... 不幸的是,另一个与json相关的问题...

Consider the following json 考虑以下json

[{"details":{
"forename":"Barack",
"surname":"Obama",
"company":"government",
"email":"bigcheese@whitehouse.com",
"files": [{
      "title":"file1","url":"somefile.pdf"
       },
       {
       "title":"file2",
       "url":"somefile.pdf"
       }]
}
}]

I need to send this data to a php script on my server and then interact with it on the server but don't know how. 我需要将此数据发送到服务器上的php脚本,然后在服务器上与之交互,但不知道如何。

I'm sending it via jquery.ajax and its being sent fine (no error messages) and heres the code. 我通过jquery.ajax发送它,并且发送得很好(没有错误消息),这里是代码。 (newJson is my json object I've created exactly as above) (newJson是我完全如上所述创建的json对象)

$.ajax({
type: "POST",
url: "test.php",
dataType: 'json',
data: newJson,
success: function(msg) 
    {
    alert(msg);
    },
error: function(jqXHR, textStatus) 
    {
    alert(textStatus);
    }
});

So in my php script so far I just want to echo back the content as a string which displays in the success alert 所以到目前为止,在我的php脚本中,我只想将内容作为字符串回显,该字符串将显示在成功警报中

<?php
header('Access-Control-Allow-Origin: *'); 
echo $_POST;
?>

but that just gives me a parse error.. so any ideas you wonderful people? 但这只是给我一个解析错误..那么,有什么好主意的人吗?

You have to have a key/value pair to receive the data in php with $_POST[key] . 您必须有一个键/值对,才能使用$_POST[key]接收php中的数据。 Sending the array you have all by itself is not best approach since you already have structure to object 独自发送所有数组不是最好的方法,因为您已经具有对象的结构

I would unwrap the outer array, since you are only sending one object inside it 我将解开外部数组,因为您仅在其中发送了一个对象

Then Object would look like 然后对象看起来像

{"details":{
"forename":"Barack",
"surname":"Obama",
"company":"government",
"email":"bigcheese@whitehouse.com",
"files": [{
      "title":"file1","url":"somefile.pdf"
       },
       {
       "title":"file2",
       "url":"somefile.pdf"
       }]
}
}

In php would receive with $_POST['details'] . 在php中会收到$_POST['details'] Don't convert to JSON, just pass the whole object to $.ajax data property. 不转换为JSON,只需将整个对象传递给$.ajax data属性。

If you get parserror from ajax, is on receiving side and sounds like either getting a 500 eror from php or not sending back json as expected by your dataType 如果您从ajax收到parserror ,则是在接收方,听起来像是从php获取500错误或未按dataType预期发送回json

First, the original JSON string is of wrong format. 首先,原始JSON字符串格式错误。 Try 尝试

{
  "details":{
    "forename":"Barack",
    "surname":"Obama",
    "company":"government",
    "email":"bigcheese@whitehouse.com",
    "files": [
      { "title":"file1","url":"somefile.pdf" },
      { "title":"file2","url":"somefile.pdf"}
    ]
  }
}

Second, the data sent to PHP has already parsed into an array, but not in JSON. 其次,发送到PHP的数据已经解析为一个数组,但不是JSON。 To echo, you must use json_encode to convert the array back to JSON string 要回显,必须使用json_encode将数组转换回JSON字符串

echo json_encode($_POST);
exit;

Since you are not passing the JSON as a field, you can do: 由于您没有将JSON作为字段传递,因此可以执行以下操作:

<?php 
  $post = file_get_contents("php://input");
  $json = json_decode($post);
  var_dump($json); // Should be a nice object for you.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM