DOJO如何处理ajax请求?

时间:2022-10-13 14:40:33

Using a form in a dialog box I am using Dojo in jsp to save the form in my database. After that request is completed using dojo.xhrPost() I am sending in another request to update a dropdown box that should include the added form object that was just saved, but for some reason the request to update the dropdown is executed before saving the form in the database even though the form save is called first. Using Firebug I can see that the getLocations() request is completed before the sendForm() request. This is the code:

在对话框中使用表单,我在jsp中使用Dojo来保存数据库中的表单。使用dojo.xhrPost()请求完成后我发送另一个请求来更新一个下拉框,应该包括添加表单对象,只是救了,但由于某种原因请求来更新下拉在数据库中保存表单执行即使表单保存被称为第一。使用Firebug,我可以看到getlocation()请求在sendForm()请求之前完成。这是代码:

<button type="button" id="submitAddTeamButton" dojoType="dijit.form.Button">Add Team
                    <script type="dojo/method" event="onClick" args="evt">

                        sendForm("ajaxResult1", "addTeamForm");
                        dijit.byId("addTeamDialog").hide();
                        getLocations("locationsTeam");
                    </script>

function sendForm(target, form){
    var targetNode =  dojo.byId(target);

    var xhrArgs = {
        form: form,
        url: "ajax",
        handleAs: "text",
        load: function(data) {

            targetNode.innerHTML = data;
        },
        error: function(error) {
            targetNode.innerHTML = "An unexpected error occurred: " + error;
        }

    }

    //Call the asynchronous xhrGet
    var deferred = dojo.xhrPost(xhrArgs);
}

function getLocations(id) {
    var targetNode =  dojo.byId(id);

    var xhrArgs = {
        url: "ajax",
        handleAs: "text",
        content: {
            location: "yes"
        },
        load: function(data) {

            targetNode.innerHTML = data;
        },
        error: function(error) {
            targetNode.innerHTML = "An unexpected error occurred: " + error;
        }
    }

    //Call the asynchronous xhrGet
    var deferred = dojo.xhrGet(xhrArgs);
}

Why is this happening? Is there way to make the first request complete first before the second executes?

为什么会这样?是否有方法使第一个请求在第二个请求执行之前完成?

To reduce the possibilities of why this is happening I tried setting the cache property in xhrGet to false but the result is still the same.

为了减少发生这种情况的可能性,我尝试在xhrGet中设置缓存属性为false,但结果仍然是一样的。

Please help!

请帮助!

2 个解决方案

#1


4  

As Alex said, asynchronous requests are just that - their order is not guaranteed. If you want to guarantee their order, you can make them synchronous if you like. There is an option sync: true I think that you can send with the request args. This causes the browser to freeze up until the request gets back, so it's not recommended unless you have no other option, and the request is very quick.

正如Alex所说,异步请求就是这样——它们的顺序没有保证。如果你想保证他们的订单,你可以让他们同步。有一个选项sync: true,我认为您可以发送请求args。这将导致浏览器在请求返回之前冻结,所以不建议使用,除非您没有其他选项,并且请求非常快。

You can also submit whatever data you need along with the data of the current request. For example, suppose dropdown A's value determines the list choices available in dropdown B. Rather than submitting a change when dropdown A is changed, then refreshing dropdown B's choices, what you can do is submit A's value at the time when dropdown B is opened, and process it in the server logic that determine's B's choices. (This assumes you have a drop-down widget with choices generated by the server, rather than a standard tag.)

您还可以根据当前请求的数据提交所需的任何数据。例如,假设下拉一个的值决定了下拉列表选择B,而不是提交更改下拉时改变,然后刷新下拉B的选择,你所要做的就是提交的值的时候下拉B被打开,和过程在服务器逻辑决定的B的选择。(这假设您有一个下拉式小部件,由服务器生成的选项,而不是标准的标签。)

#2


3  

The first A in Ajax stands for "asynchronous", which means things occur "at their own pace": most likely the requests are sent in the order you expect, but they complete the other way around simply because the second one is faster. Yes, of course you can wait to even start (send) the second request until the first one completes -- most simply, you can just put the starting of the second request in the callback function of the first.

Ajax中的第一个A代表“异步”,这意味着“按自己的速度”发生:大多数情况下,请求是按照您期望的顺序发送的,但是它们完全相反,因为第二个请求更快。当然,您当然可以等到第一个请求完成时才开始(发送)第二个请求——最简单的是,您可以将第二个请求的启动放在第一个请求的回调函数中。

#1


4  

As Alex said, asynchronous requests are just that - their order is not guaranteed. If you want to guarantee their order, you can make them synchronous if you like. There is an option sync: true I think that you can send with the request args. This causes the browser to freeze up until the request gets back, so it's not recommended unless you have no other option, and the request is very quick.

正如Alex所说,异步请求就是这样——它们的顺序没有保证。如果你想保证他们的订单,你可以让他们同步。有一个选项sync: true,我认为您可以发送请求args。这将导致浏览器在请求返回之前冻结,所以不建议使用,除非您没有其他选项,并且请求非常快。

You can also submit whatever data you need along with the data of the current request. For example, suppose dropdown A's value determines the list choices available in dropdown B. Rather than submitting a change when dropdown A is changed, then refreshing dropdown B's choices, what you can do is submit A's value at the time when dropdown B is opened, and process it in the server logic that determine's B's choices. (This assumes you have a drop-down widget with choices generated by the server, rather than a standard tag.)

您还可以根据当前请求的数据提交所需的任何数据。例如,假设下拉一个的值决定了下拉列表选择B,而不是提交更改下拉时改变,然后刷新下拉B的选择,你所要做的就是提交的值的时候下拉B被打开,和过程在服务器逻辑决定的B的选择。(这假设您有一个下拉式小部件,由服务器生成的选项,而不是标准的标签。)

#2


3  

The first A in Ajax stands for "asynchronous", which means things occur "at their own pace": most likely the requests are sent in the order you expect, but they complete the other way around simply because the second one is faster. Yes, of course you can wait to even start (send) the second request until the first one completes -- most simply, you can just put the starting of the second request in the callback function of the first.

Ajax中的第一个A代表“异步”,这意味着“按自己的速度”发生:大多数情况下,请求是按照您期望的顺序发送的,但是它们完全相反,因为第二个请求更快。当然,您当然可以等到第一个请求完成时才开始(发送)第二个请求——最简单的是,您可以将第二个请求的启动放在第一个请求的回调函数中。