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

后端 未结 14 2284
暗喜
暗喜 2020-11-21 05:07

I have a JavaScript widget which provides standard extension points. One of them is the beforecreate function. It should return false to prevent an

14条回答
  •  臣服心动
    2020-11-21 05:45

    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: '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
        });
    }
    

提交回复
热议问题