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.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?