我们有一个名为Create的API方法,它以JSON格式接收以下数据
{data:
{
"token":"73264280-be3f-4f5b",
"BuildingDisplay":"Fire Station 21 Fairburn - 19 East Broad St.",
"CallerEmail":"Jim.Parker@yahoo.com",
"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":"Jim.Parker@yahoo.com",
"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
?>
答案 0 :(得分:0)
我认为这会解决问题:
$.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();
}
});
编辑:
为了澄清,如果您需要URL中的参数,则需要使用type: "GET"
。
对于JSON,您可以对值进行字符串化,但是您希望ajax data
参数像往常一样成为对象。 jQuery将循环遍历data
中的所有键,并将它们附加到查询字符串。
请注意,您可以打包到查询字符串中的数据量有一些上限,但我认为您在现代浏览器中的数据很好。
第二次编辑:代理使用POST,因此必须是POST。