Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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.
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 …

762 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

23 Experts available now in Live!

Get 1:1 Help Now