我怎样才能jQuery来执行同步,而不是异步,Ajax请求? [英] How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
问题描述
我有一个JavaScript控件,它提供了标准的扩展点。其中之一是 beforecreate
功能。它应该被创建返回假
来prevent的项目。
I have a JavaScript widget which provides standard extension points. One of them is the beforecreate
function. It should return false
to prevent an item from being created.
我添加了一个Ajax调用这个函数使用jQuery:
I've added an Ajax call into this function using jQuery:
beforecreate: function (node, targetNode, type, to) {
jQuery.get('http://example.com/catalog/create/' + targetNode.id + '?name=' + encode(to.inp[0].value),
function (result) {
if (result.isOk == false)
alert(result.message);
});
}
不过,我想prevent从创建的项目我的小工具,所以我应该返回假
在母亲的功能,而不是回调。有没有一种方法使用jQuery或任何其它API执行同步Ajax请求?
But I want to prevent my widget from creating the item, so I should return false
in the mother-function, not in the callback. Is there a way to perform a synchronized Ajax request using jQuery or any other API?
推荐答案
从 jQuery的文档一>:您指定的同步选项是假,以获得同步Ajax请求。那么你的回调可你妈的功能进行设置前的一些数据。
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.
这是你的code会是什么样子,如果按建议修改为:
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=' + encode(to.inp[0].value),
success: function (result) {
if (result.isOk == false) alert(result.message);
},
async: false
});
}
这篇关于我怎样才能jQuery来执行同步,而不是异步,Ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!