Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajax: Integrated native datatype, for XHR2 rich responseType support #1525

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/ajax.js
Expand Up @@ -332,7 +332,8 @@ jQuery.extend({
responseFields: {
xml: "responseXML",
text: "responseText",
json: "responseJSON"
json: "responseJSON",
native: "responseNative"
},

// Data converters
Expand All @@ -349,7 +350,10 @@ jQuery.extend({
"text json": jQuery.parseJSON,

// Parse text as xml
"text xml": jQuery.parseXML
"text xml": jQuery.parseXML,

// Don't convert a native response
"* native": true
},

// For options that shouldn't be deep extended:
Expand Down
25 changes: 18 additions & 7 deletions src/ajax/xhr.js
Expand Up @@ -43,7 +43,8 @@ jQuery.ajaxTransport(function( options ) {
send: function( headers, complete ) {
var i,
xhr = options.xhr(),
id = ++xhrId;
id = ++xhrId,
responses = {};

xhr.open( options.type, options.url, options.async, options.username, options.password );

Expand Down Expand Up @@ -89,15 +90,25 @@ jQuery.ajaxTransport(function( options ) {
xhr.statusText
);
} else {
// Verify the responseType attribute to avoid InvalidStateError Exception (XHR2 Spec)
// Support: IE9
// Accessing binary-data responseText throws an exception
// (#11426)
if ( (!xhr.responseType || xhr.responseType === "text") &&
typeof xhr.responseText === "string" ) {
responses.text = xhr.responseText;
}

// The native response associated with the responseType
// Stored in the xhr.response attribute (XHR2 Spec)
if ( xhr.response ) {
responses.native = xhr.response;
}

complete(
xhrSuccessStatus[ xhr.status ] || xhr.status,
xhr.statusText,
// Support: IE9
// Accessing binary-data responseText throws an exception
// (#11426)
typeof xhr.responseText === "string" ? {
text: xhr.responseText
} : undefined,
responses,
xhr.getAllResponseHeaders()
);
}
Expand Down
Binary file added test/data/native.bin
Binary file not shown.
1 change: 1 addition & 0 deletions test/data/native.txt
@@ -0,0 +1 @@
0123
23 changes: 23 additions & 0 deletions test/unit/ajax.js
Expand Up @@ -1999,6 +1999,29 @@ module( "ajax", {
).always( start );
});

ajaxTest( "jQuery.ajax() - Native datatype", 8, [{
url: url("data/native.bin"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing spaces

xhrFields: {
responseType: "arraybuffer"
},
success: function( data, status, jqXHR ) {
strictEqual( data, jqXHR.responseNative, "The responseNative attribute contains the response" );
ok( data instanceof ArrayBuffer, "The data retrieved is an ArrayBuffer instance" );
var array = new Uint8Array( data );
strictEqual( array.length, 4, "Check for response length" );
deepEqual( [array[0], array[1], array[2], array[3]], [0, 1 ,2 , 3], "Check for response content" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing spaces inside brackets

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorrect spacing before commas

}
}, {
url: url("data/native.txt"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing spaces

dataType: "native",
success: function( data, status, jqXHR ) {
strictEqual( data, jqXHR.responseNative, "The responseNative attribute contains the response" );
ok( typeof data === "string", "The data retrieved is a string" );
strictEqual( data.length, 5, "Check for response length" );
strictEqual( data, "0123\n", "Check for response content" );
}
}]);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block has missing spaces inside parens and brackets all over the place.

//----------- jQuery.active

test( "jQuery.active", 1, function() {
Expand Down