0

我从表单收集的字段中编码了一个参数字符串encodeURIComponent(),然后使用XMLHttpRequest.

在我extract(urldecode($_POST), EXTR_SKIP);用来将参数提取到变量的服务器端 PHP 脚本中。

尽管在我添加 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() 的原因是因为我注意到如果访问者在表单中输入了一个合法的 & 符号,它会被视为服务器上的一个额外参数。

4

0 回答 0