Bug Tracker

Opened 14 years ago

Closed 14 years ago

Last modified 13 years ago

#380 closed bug (fixed)

$.getJSON() causes eval()-related errors on timeout.

Reported by: koto at webworkers d Owned by:
Priority: major Milestone:
Component: ajax Version:
Keywords: ajax timeout httpdata Cc: koto@…
Blocked by: Blocking:

Description

When $.getJSON function is used and the request timeouts, in .httpData the following code

   if ( type == "json" ) eval( "data = " + data );

gets called. data variable is empty (we never got the results from server), so this results in javascript error.

What happens is .getJSON is implemented as:

		jQuery.ajax( "GET", url, null, function(r, status) {
			if ( callback ) callback( jQuery.httpData(r,'json'), status );
		}, ifModified);

The specified callback calls httpData no matter what the status is (status variable is 'error' here not only because of timeouts).

httpData method:

	httpData: function(r,type) {
		var ct = r.getResponseHeader("content-type");
		var data = !type && ct && ct.indexOf("xml") >= 0;
		data = type == "xml" || data ? r.responseXML : r.responseText;

		// If the type is "script", eval it
		if ( type == "script" ) eval.call( window, data );

		// Get the JavaScript object, if JSON is used.
		if ( type == "json" ) eval( "data = " + data );
		
		// evaluate scripts within html
		if ( type == "html" ) $("<div>").html(data).evalScripts();

		return data;
	},

Maybe httpData should look at the readyState property of r to check if eval() (or other processing functions) should be called at all. I don't know the internals of xmlhttprequest object, but when timeout occurs, jquery calls its abort() method, so surely its state after abort() changes and there is a way to check for it.

Change History (1)

comment:1 Changed 14 years ago by joern

Resolution: fixed
Status: newclosed
Version: 1.0

As a side-effect, this was fixed while refactoring $.get. $.get now uses that success callback of $.ajax, and that is only executed when the request is successful.

Note: See TracTickets for help on using tickets.