Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-23
3
Medium Priority
?
610 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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

885 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