开发者

jquery ajax returning true but not display proper message?

I have the following code

$(document).ready(function(){
// class exists
if($('.delete').length) {
        // add click handler
    $('.delete').click(function(){
        // ask for confirmation
        var result = confirm('Are you sure you want to delete this?');
        var vid     = $(this).attr('id'); 
        var row = $(this).parents('tr');  
        // do ajax request
        if(result) {
            $.ajax({
                type:"POST",
                url:"delete_joke.php",
                data:{id:vid,garbage:'FG9g543hfh'},
                dataType: "json",
                success:function(response){

                    // hide table row on success
                    if(response == "true") {
                        row.fadeOut();
                       $('#errors').show();
                        $('#errors').html( '<div class="message green"> Successfully deleted!<img src=开发者_Go百科"http://puppetweb.ca/play/views/admin/gfx/icon-close.gif" alt="Close this item" /> </div>' ).show();

                    }else{
                       $('#errors').show();
                        $('#errors').html( '<div class="message red"> Error deleting. <img src="http://puppetweb.ca/play/views/admin/gfx/icon-close.gif" alt="Close this item" /> </div>' ).show();

                    }
                }
            });
        }
    return false;
    });
}
});

And it works up until the part where I want to show the success, I have the php script print "true" on success and "false" on error, and if I do alert(response) it shows the word true. But it is displaying the Error deleting on success and Error deleting on error?

Any help?


Try replacing if(response == "true") with if(response) if you are sure it is returning true/false.

To check the type of data response is, try alert(typeof response) and see what it shows.

Edit:

Since you mention data type as JSON, make sure you return a valid response and set the content type to json in response.

If your json return is {"status":true} then you should use if(response.status)


The problem here is scope. When the success function runs row doesn't equal anything. One of the easiest solutions is to pass back the id, exact it, use it as a selector, and hide.

If I were doing this I'd switch from json to scrip and return the JavaScript to perform. That seems more useful than true or false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜