weixin_33698043 2015-07-01 21:47 采纳率: 0%
浏览 59

AJAX xmlhttp.responseText

I've been racking my brain for a few hours and can't figure out why a string comparison won't work. In the following code, I do a xmlhttp call and get a response text. The PHP file that I get a call IS returning the proper response string "NOAD", and NOAD is being displayed when appropriate in my testing. However, when the call is returned NOAD I want to identify it, however for some reason within the call below xmlhttp.responseText == comparisonText its NOT properly comparing the two. Why does xmlhttp.responseText printout NOAD but I can't use it within the comparator?

function loadXMLAdImage1Doc(currentScenarioTime)
{

var returnText = "Not Here";
var comparisonText = "NOAD";

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
{


    if (xmlhttp.responseText == comparisonText) 
       {
            document.getElementById("AJAXTEST").innerHTML =returnText;
       } else  {

            document.getElementById("AJAXTEST").innerHTML =xmlhttp.responseText; 
            }
    }
  }
  • 写回答

1条回答 默认 最新

  • weixin_33698823 2015-07-01 23:04
    关注

    Okay, thanks Luth for pointing me in the right direction. I solved my problem, but I'm not sure what the problem is within xmlhttp.responseText or user error that caused it.

    The return text from my PHP file was placing an unseen character BEFORE the returned string (in testing, I would have figured it was an invisible character after prior to the results but go figure). Theoretically, this shouldn't be happening...my PHP is returning a straightforward string:

    ..do some MYSQL queries..
    $AdLink = mysql_result($result, 0);
    
    if ($AdLink == "") {
    
    echo "NOAD";
    } else
    {
    echo $AdLink;
    }
    

    So the PHP file SHOULD be sending a 4 character return screen to the xmlhttp.responseText call named "NOAD". But what was found in measuring the string length was that it was sending a 5 character string back with some invisible character BEFORE "NOAD" that wasn't showing up on the screen - therefore screwing up the comparator. I used the following code to remove the character and it worked perfectly...

    returnText = xmlhttp.responseText;
    var returnlength = returnText.length;
    
    returnText = returnText.substring(1, returnlength);
    

    I have no idea WHAT the character is, whether its a problem with my PHP code or the xmlhttp.responseText call, but something to be aware of if you're dealing with the call.

    评论

    报告相同问题?

    悬赏问题

    • ¥20 求各位能用我能理解的话回答超级简单的一些问题
    • ¥15 yolov5双目识别输出坐标代码报错
    • ¥15 这个代码有什么语法错误
    • ¥15 给予STM32按键中断与串口通信
    • ¥15 使用QT实现can通信
    • ¥15 关于sp验证的一些东西,求告知如何解决,
    • ¥35 关于#javascript#的问题:但是我写的只能接码数字和字符,帮我写一个解码JS问题
    • ¥15 prophet运行报错,如何解决?
    • ¥15 用GPU跑pytorch搭建的LSTM的时候出现了奇怪的报错
    • ¥20 前端数据是从session等作用域拿到的,如何取值继续传递后端呢