我需要一些JSON对象的帮助和使用它的Ajax调用。我需要传递这些数据来更新我的数据库:
JSON字符串:
[{"id":"1"},{"id":"10"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"},{"id":"7"},{"id":"8"},{"id":"9"}]
我正在使用AJAX Post。我的代码:
$.ajax({
url: "hello_world.php",
type: "POST",
data: JSON.stringify(jsonString),
processData: false,
contentType: 'application/json',
dataType: 'json',
success: function(data){
alert("Call success");
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Error: " + xhr.status + "\n" +
"Message: " + xhr.statusText + "\n" +
"Response: " + xhr.responseText + "\n" + thrownError);
}
});
PHP代码:
<?php
if(isset($_POST))
{
$json = file_get_contents('php://input');
$decoded = json_decode($json, TRUE);
echo $decoded;
}
?>
答案 0 :(得分:0)
$json = file_get_contents('php://input');
$decoded = json_decode($json, TRUE);
var_export($decoded); # for testing else use echo json_encode($decoded);