AngularJs。 $ HTTP POST不张贴的项目全部集合在一个for循环全部、项目、HTTP、AngularJs

2023-09-13 05:17:01 作者:始于脸红

我有一个集合,我需要它在后几个项目的循环。这里是code为:

I have a collection to which I need to post several items in a for loop. Here is code for that:

            for(i = 0; i < 28; i++) {
                var request = $http({
                    method: "post",
                    url: "/students",
                    data: {
                        studentName: "Student",
                        answerImage: "image", 
                        questionPrompt: 1
                    }
                }).then(function successCallback(response) {
                }, function errorCallback(response) {
                    console.log(response);
                });
              }

当我搜索的集合,它只是贴23项。我已经清除我的收藏,并试图几次,每次它只能职位20-23项。所有的数据的字段是细。这是错误响应我得到的控制台上:

When I search the collection, it only posted 23 items. I have cleared my collection and tried this several times and each time it posts only 20-23 items. All the fields for the data are fine. This is the error response I get on the console:

 Object {data: null, status: -1, config: Object, statusText: ""} 

我不知道该怎么在这里做。在我的真正的应用程序,我需要在这张贴类似的东西〜200个项目到这个集合的循环。它是一个超时的问题?

I am not sure what to do from here. In my real app, I would need to post something like ~200 items to this collection in this for loop. Is it a timing out issue?

谢谢!

推荐答案

这是可能的,你可能会因为浏览器将会速率限制多少可以并发请求做出同一个域中运行到超时这里。 通常情况下,这个数字是6,但不同取决于浏览器。

It's possible you might be running into a timeout here because the browser is going to rate limit how many concurrent requests you can make to the same domain. Typically that number is 6, but differs depending on the browser.

在一般情况下,使200多个HTTP请求立刻将是您的应用程序真正的拖累。

In general, making 200+ HTTP requests at once is going to be a real drag on your application.

有一个更好的办法是要么修改您的端点采取集合,或创建一个新的端点一样。 200对象单个请求将是比试图使200个人要求更好的性能。

A better approach would be to either modify your endpoint to take a collection, or create a new endpoint that does. A single request with 200 objects is going to be much more performant than trying to make 200 individual requests.