Skip to content

Instantly share code, notes, and snippets.

@ojak
Last active December 15, 2015 09:49
Show Gist options
  • Save ojak/5241273 to your computer and use it in GitHub Desktop.
Save ojak/5241273 to your computer and use it in GitHub Desktop.
Test for jQuery 1.9.1 .ajax() returns when 204 is encountered. jQuery Ticket: http://bugs.jquery.com/ticket/13654
--- TEST1: Test same domain:port with 'script' dataType
GET http://localhost:8080/204 204 No Content
Success
statusCode 204
=== OK
--- TEST2: Test different port with 'script' dataType
Success
statusCode 200
=== INCORRECT STATUSCODE.
--- TEST3: Test same domain:port with 'text' dataType
GET http://localhost:8080/204 204 No Content
Success
statusCode 204
=== OK
--- TEST4: Test different port with 'text' dataType
GET http://localhost:8082/204 204 No Content
Fail
- xhr.status : 0
- xhr.statusText : error
- xhr.response : undefined
- xhr.responseText :
- xhr.responseXML : undefined
- textStatus : error
- errorThrown :
=== FAIL AND INCORRECT STATUSCODE
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
// SETUP
// 1) Run a local server that returns 204 on two different ports (ex: localhost:8080 and localhost:8082).
// - Sample NGINX conf:
/*
http {
# Include default mime types
include mime.types;
server {
listen 8080;
server_name localhost;
# Serve this page
location = /204_fail.html {
autoindex on;
default_type text/html;
alias /path/to/test/file/;
}
# Error code tests
location = /200 {
return 200 '';
}
location = /204 {
return 204;
}
}
server {
listen 8082;
server_name localhost;
# Error code tests
location = /200 {
return 200 '';
}
location = /204 {
return 204;
}
}
}
*/
// 2) Open this file on first server port (ex: http://localhost:8080/204_fail.html)
// 3) Read the console readout with test results
//
$.ajax({
beforeSend: function () { console.log("--- TEST1: Test same domain:port with 'script' dataType "); },
url: 'http://localhost:8080/204', // Same domain
crossDomain: false,
dataType: 'text',
statusCode: {
200: function () { console.log("statusCode 200"); },
204: function () { console.log("statusCode 204"); }
},
complete: function () {
console.log("=== OK\n\n");
test02();
}
})
.done(function() {
console.log("Success");
})
.fail(function(xhr, textStatus) {
console.log("Fail");
//console.log("Fail - Status: " + xhr.status);
});
function test02 () {
$.ajax({
beforeSend: function () { console.log("--- TEST2: Test different port with 'script' dataType "); },
url: 'http://localhost:8082/204', // crossdomain
crossDomain: true,
dataType: 'script',
statusCode: {
200: function () { console.log("statusCode 200"); },
204: function () { console.log("statusCode 204"); }
},
complete: function () {
console.log("=== INCORRECT STATUSCODE.\n\n");
test03();
}
})
.done(function() {
console.log("Success");
})
.fail(function(xhr, textStatus) {
console.log("Fail");
//console.log("Fail - Status: " + xhr.status);
});
}
function test03 () {
$.ajax({
beforeSend: function () { console.log("--- TEST3: Test same domain:port with 'text' dataType "); },
url: 'http://localhost:8080/204', // crossdomain
crossDomain: false,
dataType: 'text',
statusCode: {
200: function () { console.log("statusCode 200"); },
204: function () { console.log("statusCode 204"); }
},
complete: function () {
console.log("=== OK\n\n");
test04();
}
})
.done(function() {
console.log("Success");
})
.fail(function(xhr, textStatus) {
console.log("Fail");
//console.log("Fail - Status: " + xhr.status);
});
}
function test04 () {
$.ajax({
beforeSend: function () { console.log("--- TEST4: Test different port with 'text' dataType "); },
url: 'http://localhost:8082/204', // crossdomain
crossDomain: true,
dataType: 'text',
statusCode: {
200: function () { console.log("statusCode 200"); },
204: function () { console.log("statusCode 204"); }
},
complete: function () {
console.log("=== FAIL AND INCORRECT STATUSCODE");
}
})
.done(function() {
console.log("Success");
})
.fail(function(xhr, textStatus, errorThrown) {
console.log("Fail");
console.log(" - xhr.status : " + xhr.status);
console.log(" - xhr.statusText : " + xhr.statusText);
console.log(" - xhr.response : " + xhr.response);
console.log(" - xhr.responseText : " + xhr.responseText);
console.log(" - xhr.responseXML : " + xhr.responseXML);
console.log(" - textStatus : " + textStatus);
console.log(" - errorThrown : " + errorThrown);
});
}
</script>
</head>
<body>
See console.
</body>
</html>
http {
# Include default mime types
include mime.types;
server {
listen 8080;
server_name localhost;
# Serve this page
location = /204_fail.html {
autoindex on;
default_type text/html;
alias /path/to/test/file/;
}
# Error code tests
location = /200 {
return 200 '';
}
location = /204 {
return 204;
}
}
server {
listen 8082;
server_name localhost;
# Error code tests
location = /200 {
return 200 '';
}
location = /204 {
return 204;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment