SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post I don't know how to alert this parameters

    Hi, I am confuse of this jquery.ajax,how can i alert this three parameters if the request get fails, i want to know what the error causes and to alert this error.
    like errorparsing,error, pagenotfound..etc...

    by reading this

    error
    Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )
    A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests. This is an Ajax Event.


    i tried this


    Code:
      $.ajax({
        type: 'post',
        url: 'notfoundpage.php',
        error: function(jqXHR,textStatus,errorThrown ){
          //here i want to alert those 3 parameters that causes the error.
         //I tried like this
    
             alert(jqXHR);
             alert(textStatus);
             alert(errorThrown);
       },
       .....
       .....
    });

    Thank you in advance.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jemz View Post
    Hi, I am confuse of this jquery.ajax,how can i alert this three parameters if the request get fails, i want to know what the error causes and to alert this error.
    It is the errorThrown variable that contains info about what caused the error.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    It is the errorThrown variable that contains info about what caused the error.
    Hi paul, thank you for the reply what about the parse error do i need to delclare each of them like this
    $.ajax({
    type: 'post',
    url: 'notfoundpage.php',
    error: function(jqXHR,textStatus,errorThrown ){

    alert(errorThrown);
    },
    error: function(jqXHR,textStatus,errorThrown ){

    alert(jqXHR);
    },
    error: function(jqXHR,textStatus,errorThrown ){

    alert(textStatus);
    },
    .....
    .....
    });

    please enligthen my mind..

    Thank you in advance

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jemz View Post
    Hi paul, thank you for the reply what about the parse error
    The parse error is not a problem with that error function.

    Instead, the "parse error" is the error message about why the ajax request is failing. Because 'notfoundpage.php' is not a valid URL.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The parse error is not a problem with that error function.

    Instead, the "parse error" is the error message about why the ajax request is failing. Because 'notfoundpage.php' is not a valid URL.

    yes, I tried the parse error message by using the textStatus, I tried to make the url as invalid so that it will alert "page not found",but if i will just alert the textStatus it will not display the page not found, my point is how can i know or to alert message error that causes my ajax request to fail..

    Thank you in advance.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jemz View Post
    it will not display the page not found, my point is how can i know or to alert message error that causes my ajax request to fail..
    The ajax request didn't even get started, so errorThrown will not have anything to show.

    (from the jQuery page):

    When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The ajax request didn't even get started, so errorThrown will not have anything to show.

    (from the jQuery page):

    When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."
    Hi paul, And how can i show such as "Not Found" or "Internal Server Error." ?

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jemz View Post
    Hi paul, And how can i show such as "Not Found" or "Internal Server Error." ?
    Aha - the information about that will be in the status property of the jqXHR object
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Aha - the information about that will be in the status property of the jqXHR object
    Hi paul, can you please show some snippet.I am confuse here.

    Thank you in advance.

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jemz View Post
    Hi paul, can you please show some snippet.I am confuse here.

    Thank you in advance.
    alert(jqXHR.status);

    That will give you the 404 error status code.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    alert(jqXHR.status);

    That will give you the 404 error status code.
    Hi paul thank you, so this will be like this ...


    Code:
      $.ajax({
       url:'myinvalidpage.php',
       data: {id:1},
       error: function(jqXHR,textStatus,errorThrown ){
                 alert(jqXHR.status);
     },
     
     });
    after fixing the invalid url to valid.
    what if the request will return one of this message
    "timeout", "error", "abort", and "parsererror"
    alert(jqXHR.status); //does this still will alert ?

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jemz View Post
    after fixing the invalid url to valid.
    what if the request will return one of this message


    alert(jqXHR.status); //does this still will alert ?
    No. The status property is only for when the web page returns a status code in relation to the page request.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  13. #13
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    No. The status property is only for when the web page returns a status code in relation to the page request.
    Hi paul, what should i do now in order to alert one of those messages
    if ever the ajax will be fails.

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jemz View Post
    Hi paul, what should i do now in order to alert one of those messages
    if ever the ajax will be fails.
    What do you want to do? That's up to you.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #15
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    What do you want to do? That's up to you.
    No i mean paul, is that if i will not change the
    alert(jqXHR.status);
    , i could not know what error message causes my ajax fails

    so if i will going do like this,after changing the correct url.

    Code:
      $.ajax({
       url:'validpage.php',
       data: {id:1},
       error: function(jqXHR,textStatus,errorThrown ){
                 alert(jqXHR.status);
                 alert(textStatus); // so this will be alerted if ever i have parseerror,or etc ?
     },
     
     });


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •