简体   繁体   中英

Why is '\n' being pre-pended to my HTTP (AJAX) responseText?

This particular AJAX call is returning "\\n" in front of the value returned by responseText.

It was previously not doing that and now when I test for a valid returned code with if (request.responseText == 100) it fails because it now equals "\\n100".

I know I could strip off the "\\n", but that would be a workaround and I would prefer to find the cause and fix it.

Here's my client-side code:

function AJAX(){

    var xmlHttp;

    try{
        xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
        return xmlHttp;
    }
    catch (e){
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
            return xmlHttp;
        }
        catch (e){
            try{
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                return xmlHttp;
            }
        catch (e){
            alert("Your browser does not support AJAX!");
            return false;
        }
        }
    }
}


function logDetails() {

    var request,
        Result  = document.getElementById('Result'),
        message = document.getElementById('message'),
        url = 'ajax/login.user.php?',
        us  = document.getElementById('username').value,
        pa  = document.getElementById('password').value;

    Result.innerHTML = 'Logging in...';

    if (document.getElementById) {

        request = AJAX();

    }

    if (request) {

        request.onreadystatechange = function() {

            if (request.readyState == 4 && request.status == 200) {

                var r = request.responseText;
//var r = 100;
                if (r == '100') {

                    Result.innerHTML        = 'You are now logged in.';
                    window.location.href    = "prebooks.php";

                }

                else if (r == '101' || r == '102') {

                    Result.innerHTML    = 'Your login attempt failed.';
                    resetDetails();

                }

                else if (r == '103') {

                    Result.innerHTML    = 'Sorry, you have no books subscription.';

                }

                else if (r == '999') {

                    Result.innerHTML    = 'You have no more attempts!';
                    message.innerHTML   = 'Please call us on (02) XXXXXXX so that we can assist you.';

                } else {

                    alert(r);

                }
            }
        };
    }
    // add my vars to url
    url += "arg1="+us+"&arg2="+pa;

    request.open("GET", url, true);
    request.send(null);
}

Here's my server-side code:

<?= 100 ?>

Ok, I simplified it, but I've tried just echoing '100' directly and the issue remains.

UPDATE I was mistaken that echoing '100' directly didn't solve the problem. It does. Sorry about that and thanks for pointing me in the right direction.

However, this does leave me with trying to find how the output is being polluted on the server-side.

On the server-side I have a class which handles the authentication and returns a value (100) to be echoed. This is the line:

echo $L->doLogin($pkg);

The lines relating to the return in the doLogin() method are:

$pkg[status]=100;
return $pkg[status];

And to be sure that a newline isn't leaking in some place, if I replace echo $L->doLogin($pkg); with echo 100; it works.

UPDATE 2 - SOLVED It turns out that the problem was in an included class file which is included within the doLogin() method, which had recently been updated to include a single line-break at the top of the file, before the opening <? .

Many thanks to everyone for your input (I'd still be fumbling around in client-side code without it)!

I had the same problem and discovered (by adding dataFilter option to Ajax with an alert show the stringified JSON returned) that it was really the PHP script which was having syntax problem. PHP server was then pre-pending an HTML mini-document to signal the error. But then, when back to AJAX, as dataType was 'json', the whole returned PHP response was json parsed first, thus stripping off all the HTML prepended and leaving only newlines. These newlines in front of valid JSON returned data was causing the JSON data to be considered syntax error, and that was it ! With dataFilter option sending the raw data in an alert, I was able to see the PHP script initial error and once corrected, no more newlines pre-pended!

i had the same problem and i understood I hit Inter several times in end of page that i incloude to my page and when i delete it my responseText show without \\n. :)

example:

_enter _enter _enter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM