Skip to content

Instantly share code, notes, and snippets.

@markelog
Created September 28, 2014 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markelog/42d43a489afb7201ddd6 to your computer and use it in GitHub Desktop.
Save markelog/42d43a489afb7201ddd6 to your computer and use it in GitHub Desktop.
More of jQuery.xhr
// See http://api.jquery.com/jQuery.ajax/#entry-examples
$.xhr( "some.php" )
.method( "POST" )
.send({ name: "John", location: "Boston" })
.then(function( xhr ) {
alert( "Data Saved:" + xhr.responseText );
});
$.xhr( "test.html" )
.cache( false )
.send()
.then(function( xhr ) {
$( "#results" ).append( xhr.responseText );
});
$.xhr( "page.php" ).send( xmlDocument ).then( handleResponse );
$.xhr( "script.php" )
.method( "POST" )
.send({ id : menuId })
.then(function( xhr ) {
$( "#log" ).html( xhr.responseText );
}).catch(function( xhr ) {
alert( "Request failed: " + xhr.status );
});
$.xhr( "test.js" ).send().then( $.xhr.as( "script" ) );
// ---
// Full featured example
$.ajax({
url: "api",
cache: false,
timeout: 1000,
type: "POST",
data: { "foo": "bar" },
headers: { "foo": "bar" },
beforeSend: function( xhr ) {
...
}
}).success(function(){
...
})
$.xhr( "api" )
.method( "POST" )
.cache( false )
.timeout( 1000 )
.header( "foo", "bar" )
.native(function( xhr ) {
...
})
.send({ "foo": "bar" })
.then(function( json ) {
...
})
// For simple requests we could use:
$.xhr.get( "foo" ).then(function() {})
$.xhr.post( "bar", {} ).then(function() {});
// But its alredy provided by
(jQuery.get) || (jQuery.post)
// So basically, i would just simplify they signatures
// by removing "success" and "dataType" options
// See http://api.jquery.com/jQuery.post/ as an example
$.get( "foo" ).then(function() {})
$.post( "bar", {} ).then(function() {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment