drzk21632 2018-08-17 15:15
浏览 58
已采纳

如何正确比较ajax post call“data”的响应

So what I am doing is I am calling a ajax post to a php file, and what that php file does it is echo's out data, which I retrieve through a variable data, the problem is when I go to compare that a string it never is successful. Here's some code:
Javascript Code

$.post("CheckStarted.php", {
            check: 10
        }, function(data, status){
            if(data.toString() == "start"){
                    JSFunct();
                }else{
                    //do something else.
                }
                console.log(data);
            });
So as you can see I am checking to see if it is equal to "start" so that will start my JSFunct, the problem is every time the console, log's start or stop and even if it logs stop(because of the console.log(data)) it will always not be equal to start even if start is logged to the console? What is going on here, and if this is not the best way to do it how would you compare this reponse. Thank you.
  • 写回答

1条回答 默认 最新

  • dtp19819 2018-08-17 15:59
    关注

    There may be white space in string check like this:-

    if( data.trim() == 'start' ) {
        //code
    }
    

    or

    if( data.search("start") != -1 ) {
        //code
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集