我們有一個名爲創建接收JSON格式的以下數據的API方法如何以部分url參數,部分json對象的形式呈現數據?
{data:
{
"token":"73264280-be3f-4f5b",
"BuildingDisplay":"Fire Station 21 Fairburn - 19 East Broad St.",
"CallerEmail":"[email protected]",
"CallerFax":"",
"CallerFirstName":"Jim",
"CallerLastName":"Parker",
"CallerMiddleInitial":"",
"CallerOtherPhone":"",
"CallerState":"",
"CallerWorkPhone":"918-354-2874"}}
可有人請說明如何獲得API接收的格式如下數據:
http://folder/users/Create?data={"BuildingDisplay":"Fire Station 21 Fairburn - 19 East Broad St.",
"CallerEmail":"[email protected]",
"CallerFax":"",
"CallerFirstName":"Jim",
"CallerLastName":"Parker",
"CallerMiddleInitial":"",
"CallerOtherPhone":"",
"CallerState":"",
"CallerWorkPhone":"918-354-2874"}&token=73264280-be3f-4f5b
非常感謝提前。
$.ajax({
type: "POST",
url: "proxyCheck.php",
data: '{data: ' + JSON.stringify(myData) + '}',
contentType: "application/json;charset=utf-8",
dataType: "json",
async: false,
success: function (response) {
alert("Record has been added successfully.");
window.location.reload();
}
});
return false;
}
//然後proxyCheck.php:
<?php
//DESCRIPTION: Allow Ajax scripts to request content from a web service they otherwise may not be able to.
$ch = curl_init("http://Users/Services/Create");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output
?>
爲什麼不在HTTP POST正文中獲取JSON數據參數?爲什麼你需要HTTP GET呢? –
@AlexP,我以爲我使用POST方法? –
所以你的問題是如何提交這個數據與'jquery'與跨域請求?當你在URL中寫這個JSON數據時,你實際上正在通過GET提交它。使用'jsonp',您可以使用GET,而不會造成太多頭痛。對於POST,您需要使服務器支持CORS。 –