简体   繁体   English

使用jQuery的.get在回调函数中处理数据不起作用

[英]process data in a callback function with jquery's .get not working

I have a strange problem with processing data coming back from a php script in javascript. 我在处理从javascript中的php脚本返回的数据时遇到一个奇怪的问题。

This is my code: 这是我的代码:

 $.get('status.php', null, resp, 'text');


 function resp(data) {

      console.error(data);

      if(data == 'ready') {

           alert(data);               
      }
 }

Now, data definitely contains 'ready' as I can see when I look at the error console (console.error(data); print me 'ready' in the console). 现在,当我查看错误控制台时,数据肯定包含“就绪”(console.error(data);在控制台中将我打印为“就绪”)。

However the if-statement does not return true and therefore the alert statement is not executed. 但是,if语句不会返回true,因此不会执行alert语句。

But why? 但为什么? I can print data, I can see it is definitely equal to 'ready', yet the statement within the if-statement never gets executed. 我可以打印数据,可以肯定它等于“就绪”,但是if语句中的语句永远不会执行。

I thought .get would just return a simple string. 我以为.get只会返回一个简单的字符串。 What am I missing? 我想念什么?

thanks for any help! 谢谢你的帮助!

may be you have extra spaces in your response, try: 可能是您的回复中有多余的空格,请尝试:

function resp(data) {
      data = $.trim( data );
      console.error(data);
      if(data == 'ready') {    
           alert(data);               
      }
 }

may be there are spaces in the text, can you try if($.trim(data) == 'ready') 文本中可能有空格,可以尝试if($.trim(data) == 'ready')

$.get('status.php', null, resp, 'text');


function resp(data) {

    console.error(':' + data + ':');

    if($.trim(data) == 'ready') {

        alert(data);               
    }
}

jQuery可以区分数据和回调,请尝试将函数作为第二个参数传递。

$.get('status.php', resp, 'text');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM