Bug Tracker

Modify

Ticket #380 (closed bug: fixed)

Opened 7 years ago

Last modified 6 years ago

$.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@…
Blocking: Blocked by:

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

comment:1 Changed 7 years ago by joern

  • Status changed from new to closed
  • Version 1.0 deleted
  • Resolution set to fixed

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.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.