简体   繁体   English

ajax没有收到与从php发送的字符串相同的字符串

[英]ajax is not receiving the same string as it is sent from php

I'm experiencing some problems when I am checking value in my ajax call which was returned from my PHP file. 当我检查从我的PHP文件返回的ajax调用中的值时,我遇到了一些问题。

Ajax call: Ajax调用:

$.ajax({
        type: "POST",
        url: "delete_record.php",
        data: {
                record_id:data[0]
              },
        cache: false,
        success: function(response) 
        {
            alert(response);                                         
            if(response=='ok')
            {
                alert("Record has been deleted");
            }
        }
        });

PHP: PHP:

//some php codeing....
//echo at the bottom
echo 'ok';

I have also tried 我也试过了

echo filter_var('ok',FILTER_SANITIZE_STRING);
echo trim('ok');

but I'm not receiving true in this line: if(response=='ok') 但我在这一行中没有得到真实: if(response=='ok')

EDIT: I forgot to wrote that I am getting ok as a response from the server script 编辑:我忘记写了我作为服务器脚本的响应得到了ok

problem can be that response has whitespace(s). 问题可能是响应有空格。 Use jQuery function $.trim : 使用jQuery函数$ .trim

 if($.trim(response)=='ok') 
 ....

You should try client side (jquery) trim instead of server side (PHP), 你应该尝试客户端(jquery)修剪而不是服务器端(PHP),

Try following script, 试试下面的脚本,

$.ajax({
        type: "POST",
        url: "delete_record.php",
        data: {
                record_id:data[0]
              },
        cache: false,
        success: function(response) 
        {
            response = $.trim(response);                                         
            if(response=='ok')
            {
                alert("Record has been deleted");
            }
        }
        });

for your reference http://api.jquery.com/jQuery.trim/ 供您参考http://api.jquery.com/jQuery.trim/

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

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