最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Function 'success' in AjaxjQuery - Stack Overflow

programmeradmin1浏览0评论

I'm having problems displaying a value from the success function of my Ajax call. My code is as follows.

$.ajax({
    type: "POST",
    url: "http://localhost/practical 8/checkuser.php",
    data: form_data,
    success: function(response)
    {
        if(response == 'success'){
            $("#errorUsername").html("<label class='error'>"+response+"</label>");
        }else if(response == 'fail'){
            $("#errorUsername").html("<label class='error'>"+response+"</label>");
        }
    }
});

My checkuser.php basically echos "succcess" or "fail".

My if and else if statements in my success function are not working. But doing

$.ajax({
    type: "POST",
    url: "http://localhost/practical 8/checkuser.php",
    data: form_data,
    success: function(response)
    {
            $("#errorUsername").html("<label class='error'>"+response+"</label>");
        }
    }
});

works perfectly. What am I doing wrong?

I'm having problems displaying a value from the success function of my Ajax call. My code is as follows.

$.ajax({
    type: "POST",
    url: "http://localhost/practical 8/checkuser.php",
    data: form_data,
    success: function(response)
    {
        if(response == 'success'){
            $("#errorUsername").html("<label class='error'>"+response+"</label>");
        }else if(response == 'fail'){
            $("#errorUsername").html("<label class='error'>"+response+"</label>");
        }
    }
});

My checkuser.php basically echos "succcess" or "fail".

My if and else if statements in my success function are not working. But doing

$.ajax({
    type: "POST",
    url: "http://localhost/practical 8/checkuser.php",
    data: form_data,
    success: function(response)
    {
            $("#errorUsername").html("<label class='error'>"+response+"</label>");
        }
    }
});

works perfectly. What am I doing wrong?

Share Improve this question edited Jan 29, 2012 at 15:33 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked May 16, 2011 at 16:30 Hriskesh AshokanHriskesh Ashokan 7815 gold badges16 silver badges28 bronze badges 1
  • It might be helpful if you include examples of what the response variable contains. – Matt Ball Commented May 16, 2011 at 16:36
Add a ment  | 

4 Answers 4

Reset to default 5

I won't critique the if branches resulting in the same code and will assume that is just there for testing purposes.

You'll want to check for extra white space being sent from the PHP script. That would explain why the javascript conditionals don't return true.

You can also trim the response with jQuery: response = jQuery.trim(response);

There could be a newline in the string. Try:

response = $.trim(response)

if(response == 'success'){
  $("#errorUsername").html("<label class='error'>"+response+"</label>");
}else if(response == 'fail'){
  $("#errorUsername").html("<label class='error'>"+response+"</label>");
}

The first argument for the success callback is the return data, followed by a text status argument. So try something like:

success: function (data, textStatus) {
    if (textStatus === "success" || textStatus === "fail") {
        // do something
    }
}

Note that a success response just means that the request succeeded, not necessarily that your code produced an error.

I agree with all the responses on this subject, however you should not use any spaces in your url's because this causes issues. Use _ instead of spaces if you want, but defiantly no spaces in your url's. Try all the remendations and it should resolve your issues.

发布评论

评论列表(0)

  1. 暂无评论