The next piece of code tries to return the response fron an ajax asynchronous call but with this we can only get undefined values:
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
// return response; // <- tried that one as well
}
});
return result;
}
var result = foo(); // always ends up being `undefined`.
The...
Recent Posts
Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts
How to: Perform an AJAX Synchronous request
From the Jquery docs: you specify the async option to be false to get a synchronous Ajax request. Then your callback can set some data before your mother function proceeds.
Here's what your code would look like if changed as suggested:
beforecreate: function(node,targetNode,type,to) {
jQuery.ajax({
url: 'http://example.com/catalog/create/'
+ targetNode.id
+ '?name='
...
Aborting AJAX request with jQuery
Most of the jQuery Ajax methods return an XMLHttpRequest (or the equivalent) object, so you can just use abort().
See the documentation:
abort Method (MSDN). Cancels the current HTTP request.
abort() (MDN). If the request has been sent already, this method will abort the request.
var xhr = $.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert(...
Example: jQuery AJAX POST with PHP
Basic usage of .ajax would look something like this:
HTML:
<form id="foo">
<label for="bar">A bar</label>
<input id="bar" name="bar" type="text" value="" />
<input type="submit" value="Send" />
</form>
JavaScript:
// variable to hold request
var request;
// bind to the submit event of our form
$("#foo").submit(function(event){
// abort any pending request
if (request)...
Subscribe to:
Posts (Atom)