SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)

    Scope of variables in JavaScript

    Hi,

    I'm using jQuery's getJSON method to get the current date from a foreign server.
    I however, should this request fail (e.g. if the server is down), I want to have my page fall back to a sensible default.

    What I'd like to do is this:
    var date = "sensible default"
    $.getJSON(url, function(data){ date = new Date(data.datetime); });
    if request was successful date will contain new Date(data.datetime);
    if request was unsuccessful date still contains "sensible_default"

    What I have is this:
    var timezone = "Europe/Berlin";
    var date = "sensible_default";
    $.getJSON("http://json-time.appspot.com/time.json?tz="+timezone+"&callback=?",
    function(data){ date = new Date(data.datetime); });
    alert(date);

    But this produces an alert of "sensible_default", despite the fact that the getJSON request fired correctly.

    Am grateful for any help.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You may want to read the docs on $.getJSON and $.ajax for errors & 404 (for $.ajax only) to help you out.

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,
    I had a look at the jQuery documentation for $.getJSON and it didn't really help.
    I found an "error" method which you can chain to your getJSON request, but it doesn't seem to work when the server answers with, for example, a 503.

    I tried this, but when the server answered with a 503, it failed silently:
    $.getJSON(url, function(data){ date = new Date(data.datetime); }).error(function() { alert("error"); });

    I would really appreciate any help on this.

  4. #4
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    http://api.jquery.com/jQuery.ajax/

    statusCode:
    A map of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:

    HTML Code:
    $.ajax({
      statusCode: {
        404: function() {
          alert('page not found');
        }
      }
    });
    If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback.

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Ok, so a look at the documentation shows that . getJSON() is a shorthand for .ajax(..) with specific parameters.
    So, I rewrote my code as this:

    Code:
    $.ajax({
      dataType: 'jsonp',
      jsonp: 'jsonp_callback',
      url: 'http://a-different-domain.com',
      success: function (json) {
        alert("It worked");
      }
    });
    This works great and, as long as the remote server is available, my site shows me a popup saying "It worked".

    What I would now like to do, is to implement some error handling for when the server returns anything other than 200 OK response code (e.g. 404 or 503)

    I read the documentation and found that $.ajax() returns a jQuery XMLHttpRequest (jqXHR) object, which provides .error(), .success(), and .complete() methods.

    These methods take a function argument that is called when the $.ajax() request terminates.

    I therefore made sure that the remote server is returning a 404 and tried:

    Code:
    $.ajax({
      dataType: 'jsonp',
      jsonp: 'jsonp_callback',
      url: 'http://a-different-domain.com',
      success: function (json) {
        alert("It worked");
      }
    }).error(function() { alert("error"); });
    I also tried:

    Code:
    $.ajax({
      dataType: 'jsonp',
      jsonp: 'jsonp_callback',
      url: 'http://a-different-domain.com',
      success: function (json) {
        alert("It worked");
      },
      statusCode: {
        404: function() {
          alert('page not found');
        }
      }
    });
    but both of these fail silently.

    What am I missing?


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
  •