AngularJs. $http post not posting all the items to

2019-07-14 08:58发布

问题:

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

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: ""} 

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?

Thanks!

回答1:

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.

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

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.