を使用してフォームによって収集されたフィールドからパラメーター文字列をエンコードし、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()を追加した理由は、訪問者がフォームに正当なアンパサンドを入力すると、それがサーバー上で追加の引数として扱われることに気づいたからです。