Solved

How to add before , fail and success state to jQuery.getJSON() function

Posted on 2015-01-23
3
404 Views
Last Modified: 2015-01-24
Hello i do have this code

		$.getJSON('json-data.php', function(data) {	
			//alert(data); //uncomment this for debug
			//alert (data.item1+" "+data.item2+" "+data.item3); //further debug
			$('#name').html(data.item1);
			$('#lastname').html(data.item2);
			$('#email').html(data.item3);
		});

Open in new window



I do need to add to it a state to fire a load image and a fail state ! and success it is already written to display content.

Thank you.
0
Comment
Question by:jaylab2
3 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 250 total points
ID: 40566123
Hi,
sorry but I did not fully understand your question.

Could you perhaps explain what your final intention of the code is?

Here a sample to add functions for done (success), fail and complete:
$(function() {
		$.getJSON('json-data.php', function(data) {	
			//alert(data); //uncomment this for debug
			//alert (data.item1+" "+data.item2+" "+data.item3); //further debug
			$('#name').html(data.item1);
			$('#lastname').html(data.item2);
			$('#email').html(data.item3);
		})
		.done(function() {alert('Fires when success');})
		.fail(function() {alert('Fires in case of error in the AJAX call');})
		.always(function(){alert('Call complete');})
		});

Open in new window


HTH
Rainer
0
 
LVL 43

Assisted Solution

by:Rob
Rob earned 250 total points
ID: 40566193
And you can always use the full version of the ajax function:

$.ajax({
    dataType: "json",
    url: "json-data.php",
    method: "GET",
    success: function(data) {},
    error: function() {},
    beforeSend: function () {}
...
});
0
 

Author Closing Comment

by:jaylab2
ID: 40567917
Thank you this was very helpful
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question