Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

I have a JavaScript widget that gives standard extension points. One of them is the before creating function. It should return bogus to keep a thing from being made.

I've added an Ajax call into this capacity utilizing jQuery:

beforecreate: function (node, targetNode, type, to) {
jQuery.get('http://example /catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value),

function (result) {
if (result.isOk == false)
alert(result.message);
});
}


Be that as it may, I need to keep my gadget from making the thing, so I should return bogus in the mother-function, not in the callback. Is there an approach to play out a simultaneous AJAX demand utilizing jQuery or some other in-program API?
by

2 Answers

aashaykumar
From the jQuery documentation: you specify the asynchronous 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: 'example_url/catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value),
success: function (result) {
if (result.isOk == false) alert(result.message);
},
async: false
});
}
kshitijrana14
Try this :
function getURL(url){
return $.ajax({
type: "GET",
url: url,
cache: false,
async: false
}).responseText;
}
//example use
var msg=getURL("message.php");
alert(msg);

Login / Signup to Answer the Question.