简体   繁体   中英

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.

Ajax call:

$.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:

//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')

EDIT: I forgot to wrote that I am getting ok as a response from the server script

problem can be that response has whitespace(s). Use jQuery function $.trim :

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

You should try client side (jquery) trim instead of server side (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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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