Changeset 5614
- Timestamp:
- 05/15/08 21:03:31 (7 months ago)
- Files:
-
- 1 modified
-
trunk/jquery/src/ajax.js (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/jquery/src/ajax.js
r5605 r5614 275 275 // Create the request object; Microsoft failed to properly 276 276 // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available 277 var x ml= window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();277 var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); 278 278 279 279 // Open the socket 280 280 // Passing null username, generates a login popup on Opera (#2865) 281 281 if( s.username ) 282 x ml.open(type, s.url, s.async, s.username, s.password);282 xhr.open(type, s.url, s.async, s.username, s.password); 283 283 else 284 x ml.open(type, s.url, s.async);284 xhr.open(type, s.url, s.async); 285 285 286 286 // Need an extra try/catch for cross domain requests in Firefox 3 … … 288 288 // Set the correct header, if data is being sent 289 289 if ( s.data ) 290 x ml.setRequestHeader("Content-Type", s.contentType);290 xhr.setRequestHeader("Content-Type", s.contentType); 291 291 292 292 // Set the If-Modified-Since header, if ifModified mode. 293 293 if ( s.ifModified ) 294 x ml.setRequestHeader("If-Modified-Since",294 xhr.setRequestHeader("If-Modified-Since", 295 295 jQuery.lastModified[s.url] || "Thu, 01 Jan 1970 00:00:00 GMT" ); 296 296 297 297 // Set header so the called script knows that it's an XMLHttpRequest 298 x ml.setRequestHeader("X-Requested-With", "XMLHttpRequest");298 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); 299 299 300 300 // Set the Accepts header for the server, depending on the dataType 301 x ml.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?301 xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ? 302 302 s.accepts[ s.dataType ] + ", */*" : 303 303 s.accepts._default ); … … 305 305 306 306 // Allow custom headers/mimetypes 307 if ( s.beforeSend && s.beforeSend(x ml, s) === false ) {307 if ( s.beforeSend && s.beforeSend(xhr, s) === false ) { 308 308 // cleanup active request counter 309 309 s.global && jQuery.active--; 310 310 // close opended socket 311 x ml.abort();311 xhr.abort(); 312 312 return false; 313 313 } 314 314 315 315 if ( s.global ) 316 jQuery.event.trigger("ajaxSend", [x ml, s]);316 jQuery.event.trigger("ajaxSend", [xhr, s]); 317 317 318 318 // Wait for a response to come back 319 319 var onreadystatechange = function(isTimeout){ 320 320 // The transfer is complete and the data is available, or the request timed out 321 if ( !requestDone && x ml && (xml.readyState == 4 || isTimeout == "timeout") ) {321 if ( !requestDone && xhr && (xhr.readyState == 4 || isTimeout == "timeout") ) { 322 322 requestDone = true; 323 323 … … 329 329 330 330 status = isTimeout == "timeout" && "timeout" || 331 !jQuery.httpSuccess( x ml) && "error" ||332 s.ifModified && jQuery.httpNotModified( x ml, s.url ) && "notmodified" ||331 !jQuery.httpSuccess( xhr ) && "error" || 332 s.ifModified && jQuery.httpNotModified( xhr, s.url ) && "notmodified" || 333 333 "success"; 334 334 … … 337 337 try { 338 338 // process the data (runs the xml through httpData regardless of callback) 339 data = jQuery.httpData( x ml, s.dataType );339 data = jQuery.httpData( xhr, s.dataType ); 340 340 } catch(e) { 341 341 status = "parsererror"; … … 348 348 var modRes; 349 349 try { 350 modRes = x ml.getResponseHeader("Last-Modified");350 modRes = xhr.getResponseHeader("Last-Modified"); 351 351 } catch(e) {} // swallow exception thrown by FF if header is not available 352 352 … … 358 358 success(); 359 359 } else 360 jQuery.handleError(s, x ml, status);360 jQuery.handleError(s, xhr, status); 361 361 362 362 // Fire the complete handlers … … 365 365 // Stop memory leaks 366 366 if ( s.async ) 367 x ml= null;367 xhr = null; 368 368 } 369 369 }; … … 377 377 setTimeout(function(){ 378 378 // Check to see if the request is still happening 379 if ( x ml) {379 if ( xhr ) { 380 380 // Cancel the request 381 x ml.abort();381 xhr.abort(); 382 382 383 383 if( !requestDone ) … … 389 389 // Send the data 390 390 try { 391 x ml.send(s.data);391 xhr.send(s.data); 392 392 } catch(e) { 393 jQuery.handleError(s, x ml, null, e);393 jQuery.handleError(s, xhr, null, e); 394 394 } 395 395 … … 405 405 // Fire the global callback 406 406 if ( s.global ) 407 jQuery.event.trigger( "ajaxSuccess", [x ml, s] );407 jQuery.event.trigger( "ajaxSuccess", [xhr, s] ); 408 408 } 409 409 … … 411 411 // Process result 412 412 if ( s.complete ) 413 s.complete(x ml, status);413 s.complete(xhr, status); 414 414 415 415 // The request was completed 416 416 if ( s.global ) 417 jQuery.event.trigger( "ajaxComplete", [x ml, s] );417 jQuery.event.trigger( "ajaxComplete", [xhr, s] ); 418 418 419 419 // Handle the global AJAX counter … … 423 423 424 424 // return XMLHttpRequest to allow aborting the request etc. 425 return x ml;426 }, 427 428 handleError: function( s, x ml, status, e ) {425 return xhr; 426 }, 427 428 handleError: function( s, xhr, status, e ) { 429 429 // If a local callback was specified, fire it 430 if ( s.error ) s.error( x ml, status, e );430 if ( s.error ) s.error( xhr, status, e ); 431 431 432 432 // Fire the global callback 433 433 if ( s.global ) 434 jQuery.event.trigger( "ajaxError", [x ml, s, e] );434 jQuery.event.trigger( "ajaxError", [xhr, s, e] ); 435 435 }, 436 436 … … 439 439 440 440 // Determines if an XMLHttpRequest was successful or not 441 httpSuccess: function( r ) {441 httpSuccess: function( xhr ) { 442 442 try { 443 443 // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450 444 return ! r.status && location.protocol == "file:" ||445 ( r.status >= 200 && r.status < 300 ) || r.status == 304 ||r.status == 1223 ||446 jQuery.browser.safari && r.status == undefined;444 return !xhr.status && location.protocol == "file:" || 445 ( xhr.status >= 200 && xhr.status < 300 ) || xhr.status == 304 || xhr.status == 1223 || 446 jQuery.browser.safari && xhr.status == undefined; 447 447 } catch(e){} 448 448 return false; … … 450 450 451 451 // Determines if an XMLHttpRequest returns NotModified 452 httpNotModified: function( x ml, url ) {452 httpNotModified: function( xhr, url ) { 453 453 try { 454 var x mlRes = xml.getResponseHeader("Last-Modified");454 var xhrRes = xhr.getResponseHeader("Last-Modified"); 455 455 456 456 // Firefox always returns 200. check Last-Modified date 457 return x ml.status == 304 || xmlRes == jQuery.lastModified[url] ||458 jQuery.browser.safari && x ml.status == undefined;457 return xhr.status == 304 || xhrRes == jQuery.lastModified[url] || 458 jQuery.browser.safari && xhr.status == undefined; 459 459 } catch(e){} 460 460 return false; 461 461 }, 462 462 463 httpData: function( r, type ) {464 var ct = r.getResponseHeader("content-type"),463 httpData: function( xhr, type ) { 464 var ct = xhr.getResponseHeader("content-type"), 465 465 xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0, 466 data = xml ? r.responseXML :r.responseText;466 data = xml ? xhr.responseXML : xhr.responseText; 467 467 468 468 if ( xml && data.documentElement.tagName == "parsererror" )
