我有这个执行ajax POST的功能:
function consultaruserID(){
if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var userID = document.getElementById('userID').value;
var captcha = document.getElementById('captcha').value;
var token = encodeURIComponent(cToken);
var params = "userID="+userID + "&captcha="+captcha + "&token="+token;
xmlhttp.open('POST','/sys/getdata.php',false);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
alert (xmlhttp.responseText);
}
请注意,字符串标记的长度约为2000,并且涉及一些特殊字符。因此我使用 encodeURIComponent(cToken);
getdata.php处理所有数据并返回一个字符串。 100%工作。
现在,使用下面的代码在发布后返回SUCCESS,但它不会给我带来getdata.php预期的字符串。
$.post("/sys/getdata.php",
{userID: $('#userID').val(),
captcha: $('#captcha').val(),
token: encodeURIComponent(cToken)
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
}
);
在getdata.php上我调试了来自两种方法的ajax的$ _POST。两种方法似乎都正确发布。但是,处理令牌的功能(不是我的)会以某种方式拒绝来自ajax。
我在这里错过了什么吗?
cToken的样本:
var cToken='RadStyleSheetManager1_TSSM=&RadScriptManager1_TSM=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3aen-US%3a33363de7-7c08-435e-ab35-682b2ed2c688%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%3aen-US%3a4701e229-f1c8-4ec4-9c40-b2d233d95d5d%3a16e4e7cd%3af7645509%3a22a6274a%3aed16cbdc%3a11e117d7&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKLTc1OTk5NDIwOA8WAh4IcHJldkdVSUQFJDlkYWRiODRjLWYyN2YtNDA2Yi04MjE4LWM3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SBjb2RlIGZyb20gdGhlIGltYWdlHwMCAmRkGAIFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtSYWRDYXB0Y2hhMQULUmFkQ2FwdGNoYTEPFCsAAgUkOWRhZGI4NGMtZjI3Zi00MDZiLTgyMTgtYzcwYjg3NjgyYjU3BgAAAAAAAAAAZC71hndqclnXtz26igXAh8hMTN8v1xbnYlEz5BpnTD2e&__EVENTVALIDATION=%2FwEWAgKQq574CQLYv5ykDAZWk768pXDUic2zwz0szJjtC6%2ByEIHnfkVFizDz0zTL&RadCaptcha1_ClientState=&RadCaptcha1%24CaptchaTextBox=';
答案 0 :(得分:1)
您不应该对cToken
进行URI编码。当jQuery将对象文字转换为查询字符串时,它将自动应用URI编码。
答案 1 :(得分:0)
您的令牌似乎包含未正确传递的额外参数,请尝试传递数据字符串:
var userID = document.getElementById('userID').value;
var captcha = document.getElementById('captcha').value;
var token = encodeURIComponent(cToken);
var params = "userID="+userID + "&captcha="+captcha + "&token="+token;
$.post("/sys/getdata.php",
params,
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
}
);