6

I am changing my scripts to jquery at the moment. This is my old javascript:

var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : (window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : false);
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);

function updatePage() {
        if (request.readyState == 4) {
                if (request.status == 200) {
                    var response = request.responseText;
                    doSomething();
                } else if (request.status == 304) {
                    doSomethingElse();
                } else {
                }
        }
}

I now want to change this to jquery-ajax:

request = $.ajax({
    type: "GET",
    url: url,
    data: data,
    success: updatePage
});

How can I test for the status code (and the responseText) returned by the request, like I did in my old script?

2 Answers 2

21

try this:

request = $.ajax({
    type: "GET",
    url: url,
    data: data,
    statusCode: {
      200: function() {
        doSomething();
       },
      304:function(){
        doSomethingElse();
       }
     }
});

or:

request = $.ajax({
    type: "GET",
    url: url,
    data: data,
    complete: function(e, xhr, settings){
       if(e.status === 200){

       }else if(e.status === 304){

       }else{

       }
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

See my edit above, I really need the status code. Sorry if I didn't point that out correctly before.
How am I going to get the responseText now?
@Zulakis 200: function(dataText) { or complete:...text = e.responseText
Hmm, i tried statusCode: { 200: function() { alert('Status 200'); }, 304: function(){ alert('Status 304'); } } but it is always alerting 'Status 200' even though I can clearly see that the Status is 304 in firebug.
Same with complete: function(e, xhr, settings){ if(e.status === 200){ alert('Status 200'); }else if(e.status === 304){ alert('Status 304'); } }. Always alerts Status 200.
|
1

You need to just bind this request to an event like button click or something Like

$('#btn').click({function(){
$.ajax({
    type: "GET",
    url: url,
    data: data,
    success: updatePage
});
});

You dont need to really keep this request against a variable as jQuery ajax has a success callback which will help you to execute post success code

3 Comments

@Zulakis It also has a before handler which can handle your code before execution and success call back will handle after the execution.You still need other statuses?
Yes i need the other statuses as you can see in my question.
And I downvoted this because I was clearly asking for the status code in my question... Feel free to provide a answer to my question and I'm going to upvote you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.