Solved

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

Posted on 2015-01-23
3
367 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 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate values in an array 18 32
unset shopping cart session 15 29
get domain with php 7 19
Making register page log you in after registration is complete. 2 13
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

932 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now