ajax xhr.responseText 当结果包含@error uncaught

我通过 new XMLHttpRequest() 进行 ajax 调用;


当我这样做时


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);

它很好并且运行良好.....但是当结果包含@时我在控制台日志中收到此错误


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

澄清:例如:


当 php 给出<img src="..." />-->> 没关系


当 php 给出some text blablabla ...blalbla contact allo@test.com-->> 我得到错误


为什么我需要修复它???


慕尼黑5688855
浏览 101回答 2
2回答

慕姐8265434

与在 ajax 中一样,您发送的是纯 html 代码。我建议你在 ajax 响应中使用 json 格式。这将解决你的问题

明月笑刀无情

我找到了一个“可行”的解决方案在 ajax 调用的 php 中,我只执行一个回显并将任何出现的 @ 替换为 &commat$stuff = ...; // gathering what to echo$stuff .= ...; // gathering what to echo$stuff = str_replace('@','&commat;',$stuff);&nbsp;echo $stuff;这样浏览器显示@并且错误不会出现在控制台日志中注意:我尝试在 javascript 级别 (e.currentTarget.responseText) 中进行替换,但没有成功。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript