Search code examples
javascriptjqueryajaxxmlhttprequest

ajax xhr.responseText when result contains @ error uncaught typeerror


I do ajax call via new XMLHttpRequest();

when I do this

var params = "x="+encodeURIComponent(x);
var xhr = new XMLHttpRequest(); 
xhr.open("POST", "....php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function(e) {
    if (xhr.readyState == 4) {
        $("#divID").html(e.currentTarget.responseText);
    }
}
xhr.send(params);

It is fine and working well..... but when the result contains @ I get this error in console log

Uncaught TypeError: Cannot read property 'removeChild' of null without using removeChild

to clarify : example :

when the php gives <img src="..." /> -->> it's ok

when the php gives some text blablabla ...blalbla contact [email protected] -->> I get the error

why and do I need to fix it ???

Regards


Solution

  • I found a "workable" solution

    in the php of the ajax call I do only one echo and replace any occurence of @ to &commat

    $stuff = ...; // gathering what to echo
    $stuff .= ...; // gathering what to echo
    $stuff = str_replace('@','&commat;',$stuff); 
    echo $stuff;
    

    that way the browser shows @ and the error don't appear in console log

    note : I tried to replace in javascript level (e.currentTarget.responseText) but it didn't worked.