Skip to main content

Bug Tracker

Side navigation

#380 closed bug (fixed)

Opened November 11, 2006 12:30AM UTC

Closed November 11, 2006 11:49AM UTC

Last modified June 20, 2007 02:12AM UTC

$.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@webworkers.pl
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.

Attachments (0)
Change History (1)

Changed November 11, 2006 11:49AM UTC by joern comment:1

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.