我使用encodeURIComponent()
从表单收集的字段中编码了一个参数字符串,然后使用XMLHttpRequest
将其发送到服务器。
在服务器端PHP脚本中,我使用extract(urldecode($_POST), EXTR_SKIP);
将参数提取到变量。
虽然在添加URI组件的编码之前该过程正常工作,但现在没有在服务器上提取变量。我做错了什么?
这是执行发送的JavaScript:
function postData(url, parameters, postprocess, displayURL, runOnError) {
// Check whether user action has already failed validation
if (typeof cancel !== 'undefined') {
// Deleting a variable only works if a global
delete cancel;
return false;
}
var
xmlHttp = AJAX(),
resp,
defcon,
json;
parameters = EncodeURIComponent(parameters);
if( url.charAt( 0 ) === '&' ) {
url = url.slice( 1 );
}
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4) {
resp = xmlHttp.responseText;
checkSessionTimeout(resp);
// REDIRECT TO POST-PROCESSORS
if (postprocess !== '') {
// Goto custom post processing function
eval(unescape(postprocess));
}
// Determine whether json response or not
else if ( resp.charAt( 0 ) == '{' ) {
// it's json so redirect to...
jsonGenericRespHandler(resp, displayURL);
} else {
// Use generic post process
/* The following is depreciated. We should
* be using jsonGenericRespHandler(resp).
*/
if (resp.toLowerCase().indexOf('error') > -1) defcon = 'error';
else if (resp.toLowerCase().indexOf('success') > -1) defcon = 'success';
else if (resp.toLowerCase().indexOf('warn') > -1) defcon = 'warn';
else defcon = 'info';
// Make sure we don't miss an important message.
if (defcon === 'warn' || defcon === 'error') {
alert(resp);
eval(runOnError);
} else {
// Redirect to dashboard
if (displayURL === '') {
ajaxLoader('main/home.php', 'contentMain');
} else if (displayURL !== 0) {
ajaxLoader(displayURL, 'contentMain');
}
showMessage(defcon, resp);
}
}
}
}
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", parameters.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(parameters);
}
我应该提一下我添加encodeURIComponent()的原因是因为我注意到如果访问者在表单中输入了一个合法的&符号,那么它就被视为服务器上的额外参数。