我怎样才能jQuery来执行同步,而不是异步,Ajax请求? [英] How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

查看:184
本文介绍了我怎样才能jQuery来执行同步,而不是异步,Ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆