Php 使用json的AJAX Post调用
我需要一些关于JSON对象和使用它的Ajax调用的帮助。出于某种目的,我需要传递此数据以更新我的数据库: JSON字符串:Php 使用json的AJAX Post调用,php,ajax,json,Php,Ajax,Json,我需要一些关于JSON对象和使用它的Ajax调用的帮助。出于某种目的,我需要传递此数据以更新我的数据库: JSON字符串: [{"id":"1"},{"id":"10"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"},{"id":"7"},{"id":"8"},{"id":"9"}] 我正在使用ajaxpost。我的代码: $.ajax({ url: "hello_world.php", type: "POST",
[{"id":"1"},{"id":"10"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"},{"id":"7"},{"id":"8"},{"id":"9"}]
我正在使用ajaxpost。我的代码:
$.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;
}
?>
data:{data:jsonString}
可能有助于在php端处理数据时发送包含关键数据的对象。在发送ajax之前,请先对数据进行字符串化
$json = file_get_contents('php://input');
$decoded = json_decode($json, TRUE);
var_export($decoded); # for testing else use echo json_encode($decoded);