无法测试 AJAX ResponseText
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果确实没有比我们在这里看到的更多的内容,则您的字符串中必须包含空格或其他不可打印的字符。在进行比较之前尝试修剪它。
编辑 另外,请尝试将控制台日志记录放在“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.
请尝试以下操作:
Try the following: