我正在构建一个API,我需要一些帮助,以了解如何解析来自JSON请求的数据,下面是我的代码:
<textarea style="width: 100%; height: 300px;" id="request_json">
{
"requestType":"TourListRequest",
"data":{
"ApiKey":"12345",
"ResellerId":"999",
"SupplierId":"999",
"ExternalReference":"12345",
"Timestamp":"2013-12-10T13:30:54.616+10:00",
"Extension":{
"any":{
}
},
"Parameter":{
"Name":{
"0":" "
},
"Value":{
}
}
}
}
</textarea>
<script>
function SubmitAPI() {
var sendInfo = {
JSON : $('#request_json').val(),
URL : $('#supplier_api_endpoint_JSON_Tour_List').val(),
Type : 'JSON Tour List'
};
$('#response_json').html("Calling API...");
$.ajax({
url: 'post_JSON.php',
type: "POST",
data: JSON.stringify(sendInfo), // send the string directly
success: function(response){
var obj = jQuery.parseJSON( response );
$('#response_json').html(response);
$('#response_validation').html( obj.json_valid );
},
error: function(response) {
$('#response_json').html(response);
}
});
}
</script>因此,我需要知道如何在我的php脚本post_JSON.php中接收“sendInfo(SendInfo)”
知道吗?
提前谢谢你,
发布于 2014-04-06 05:26:03
我想你应该给你命名数据字符串,就像.
data: {info: JSON.stringify(sendInfo)},在您的php中:
$json_data = $_POST['info'];
var_dump(json_decode($json_data, true));若要使用php获取该数据,请执行以下操作:
$postedData = json_decode($json_data, true); // turns json string into an object
$requestType = $postedData['requestType']; 如果需要用jquery解析返回的json字符串,可以执行如下操作:
var jsonStuff = jQuery.parseJSON( data );
alert( jsonStuff.requestType);发布于 2014-04-06 05:03:33
我不知道php,但我认为您必须执行以下操作。
在post_JSON.php中,您可以这样做:json_decode。
https://stackoverflow.com/questions/22890168
复制相似问题