无法测试 AJAX ResponseText

发布于 2024-12-15 10:53:08 字数 328 浏览 2 评论 0原文

我的 PHP 代码使用不同的字符串响应 AJAX 调用,具体取决于...所以我想测试响应,但我无法做到。我发现responseText是未定义的类型,所以尝试了类型转换:

if (String(xmlhttp.responseText)=="OK")
{
    // do something
} else
{
    // display the responseText
    document.getElementById(spanID).innerHTML=xmlhttp.responseText;
}

猜猜会显示什么?好的。 (没有类型转换时相同)。为什么?

My PHP code responds to AJAX call with different strings, depending ... so I want to test the response, but I haven't been able to do it. I found that the responseText was of undefined type so tried type casting:

if (String(xmlhttp.responseText)=="OK")
{
    // do something
} else
{
    // display the responseText
    document.getElementById(spanID).innerHTML=xmlhttp.responseText;
}

Guess what gets displayed? OK. (Same without typecasting). Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酸甜透明夹心 2024-12-22 10:53:08

如果确实没有比我们在这里看到的更多的内容,则您的字符串中必须包含空格或其他不可打印的字符。在进行比较之前尝试修剪它。

编辑 另外,请尝试将控制台日志记录放在“if”和“else”块中 - 也许您的方法被意外调用多次,并且存在奇怪的竞争条件。

If there really isn't more than we're seeing here, your string must either have whitespace or other non printable characters in it. Try trimming it before doing the comparison.

Edit Also try putting console logging in both your 'if' and 'else' blocks - maybe your method is being called multiple times unexpectedly and there is an odd race condition.

无尽的现实 2024-12-22 10:53:08

请尝试以下操作:

var text = xmlhttp.responseText.trim();//use trim()

if (text=="OK"){
    // do something
}else{
    // display the responseText
    document.getElementById(spanID).innerHTML=xmlhttp.responseText;
}

Try the following:

var text = xmlhttp.responseText.trim();//use trim()

if (text=="OK"){
    // do something
}else{
    // display the responseText
    document.getElementById(spanID).innerHTML=xmlhttp.responseText;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文