Dave's Brain

Browse - Programming Tips - How can I reliably get some json from a server?

Date: 2014nov23
Library: jQuery
Language: javaScript


Q.  How can I reliably get some json from a server?

A.  jQuery has $.get() but its not good enough.  Any production
code needs to use $.ajax() like this:

$.ajax({ type: 'GET', // GET is the default but I like to specify it url: 'http://example.com/mything.php', // Put your URL here dataType: 'json', cache: false, // Stops the browser from caching, $.get() doesn't do this success: function(data) { // Do something with data }, error: function(jqXHR, textStatus, errorThrown) { // $.get() doesn't do this either. You need to handle errors alert('Error ' + textStatus + ' ' + errorThrown); // Or console.log() }, });

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.