// Creating variables to hold data from textboxes. First building associated details
myData.Token = $("#token").val();
myData.BuildingDisplay = $("#BuildingDisplay").val();
myData.FeatureID = $("#FeatureID").val();
myData.BuildingID = $("#BuildingID").val();
myData.Address = $("#Address").val();
myData.City = $("#City").val();
myData.District = $("#District").val();
myData.Location = $("#Location").val();
myData.State = $("#State").val();
myData.StreetName = $("#StreetName").val();
myData.Zip = $("#Zip").val();
myData.X = $("#X").val();
myData.Y = $("#Y").val();
$.ajax({
type: "POST",
url: "proxyCheck.php",
data: "{ 'Token': '" + mytoken + "','Address': '" + myaddress + "', 'City': '" + mycity + "','Location': '" + mylocation + "','State': '" + mystate + "', 'StreetName': '" + mystreetname + "', 'Zip': '" + myzip + "', 'X': '" + myX + "', 'Y': '" + myY + "'}",
contentType: "application/json;charset=utf-8",
dataType: "json",
async: false,
success: function (response) {
alert("Record has been added successfully.");
window.location.reload();
}
});
当以上数据格式化为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"}}
我想要做的是将它们传递给proxyCheck.php,如下所示:
proxyCheck.php?data={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"}}&token=73264280-be3f-4f5b
请注意& token = 73264280-be3f-4f5b
我似乎无法弄清楚如何做到这一点。
有人可以帮忙吗?
$.ajax({
type: "POST",
url: "proxyCheck.php?token=" + myData.Token,
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
$ch = curl_init("http://domain/UserServices/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
?>
// * ** * ** * ** * ** * ** * ** *
// AJAX
var uname = $("#user").val();
var upass = $("#pass").val();
$.post("proxyValidate.php",
{ data: JSON.stringify({ LoginName: uname,Password: upass }) })
.done(function(data) {
var result = JSON.parse(data);
switch(result.Status) {
case 0:
//login successful
tokenVal = result.Value.Token;
location.href = "http://domain/userService.php?token="+tokenVal;
break;
case 2:
//invalid login
alert(result.Message);
break;
}
})
.fail(function() {
alert("The AJAX request failed!");
});
});
// proxyValidate
<?php
$ch = curl_init("http://domain/Validation/Validate");
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 :(得分:2)
没有必要将javascript对象转换为字符串--jQuery将为您执行此操作。试试这个......
$.ajax({
type: "POST",
url: "proxyCheck.php?token=" + myData.Token,
data: myData,
dataType: "json",
async: false,
success: function (response) {
alert("Record has been added successfully.");
window.location.reload();
}
});
变量名称区分大小写(例如令牌和令牌),但上面的代码应该发布您需要的内容,并在URL中包含令牌值。
或者,如果您需要将数据作为字符串传递,那么您可以执行以下操作...
$.ajax({
type: "POST",
url: "proxyCheck.php?token=" + myData.Token,
data: {
data: JSON.stringify(myData)
},
dataType: "json",
async: false,
success: function (response) {
alert("Record has been added successfully.");
window.location.reload();
}
});
答案 1 :(得分:1)
对于任何可能遇到与我类似问题的人来说,这是适用于我的解决方案:
$.ajax({
type: "POST",
url: "proxyCheck.php?token",
data: {
data: JSON.stringify(myData),
token: myData.Token
},
dataType: "json",
async: false,
success: function (response) {
alert("Record has been added successfully.");
window.location.reload();
}
});
答案 2 :(得分:-1)
你真的应该使用POST来获取这类数据。如果你坚持按照你的建议使用GET,你应该对JSON进行urlencode,但是,你可能很容易发现你的url太长而无法打开。
编辑:@Archer
应该有效的解决方案$.ajax({
type: "POST",
url: "proxyCheck.php?token=" + myData.Token,
data: {
data: JSON.stringify(myData)
},
dataType: "json",
async: false,
success: function (response) {
alert("Record has been added successfully.");
window.location.reload();
}
});