Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.