Axios发布请求已停滞

时间:2019-10-25 19:21:22

标签: javascript node.js reactjs express axios

我正在for循环内使用Axios发出POST请求,以批量创建销售线索。潜在客户已经创建,但我要注意的问题是,即使我得到200响应,在网络标签页中的时间安排下,请求实际上从未完成,它们只是说“停滞不前”。

下面是我在说什么的屏幕截图 enter image description here

这可能会成为问题,因为如果我执行其他类似的POST请求,则并非所有请求都会通过。我想我在某处读到Google Chrome仅允许打开最多6个连接或类似的连接。如果这些请求停滞了,我只能假定那些连接保持打开状态,这就是导致这种情况发生的原因。

所以我想我的问题是,将这个POST请求放入for循环中而不会导致请求停滞的最佳方法是什么?我应该使它异步,如果可以的话,如何用我的代码做到这一点?

这是我的server.js

app.get('/create*', (request, response) => {
    var baseurl = 'https://OURACC.nocrm.io/api/v2/'
    var apikey = 'APIKEY'

    var title = "";
    var description = "";
    var tags = [];
    var pathname = request.url; // retrieves the requested pathname (ie, /crm/path)

    pathname = pathname.split('?');
    pathname = pathname[0].split('/')
    var parameters = request.query.params;

    var path = pathname[2]; // gets request path for the crm

    title = parameters.title
    description = parameters.description
    tags = parameters.tags

    var params = {
        title: title,
        description: description,
        tags: tags
    }


    if(path === 'leads'){

           // console.log("Inside Lead Creation");
            axios.post(baseurl + path,
                params
            ,{
                headers: {'X-API-KEY': apikey, content_type: "json", accept: "application/json"} 
            }).then(function(res){
                console.log("Lead Created");
            }).catch(function(error){
                console.log("Error: " + error);
            }) 
    }

})

在这里我称之为客户端

CreateLeads(){
        var prospects = this.state.Prospects;
        var description = "";
        var title = "";
        var tags = [];



        for(var i = 0; i < prospects.length; i++){
            tags = prospects[i].tags;
            title = prospects[i].leadName;
            description = "Business Name: " + prospects[i].businessName + "\n" +
                            "Customer Number: " + prospects[i].custNumber + "\n" +
                            "Corporate Name: " + prospects[i].corpName + "\n" +
                            "Corporate Contact Name: " + prospects[i].corpContName + "\n" +
                            "Corporate Phone: " + prospects[i].corpPhone + "\n" +
                            "Corporate Email: " + prospects[i].corpEmail + "\n" +
                            "Customer First Name: " + prospects[i].custFirst + "\n" +
                            "Customer Last Name: " + prospects[i].custLast + "\n" +
                            "Street: " + prospects[i].street + "\n" +
                            "City: " + prospects[i].city + "\n" +
                            "State: " + prospects[i].state + "\n" +
                            "Zip: " + prospects[i].zip + "\n" +
                            "Phone 1: " + prospects[i].phone + "\n" +
                            "Category: " + prospects[i].category;

            var data = {
                title: title,
                description: description,
                tags: tags,
            }

            console.log(data);
            $.get('/create/leads', {params: data}).then(response => {
                console.log(response);
            }).catch(error => {
                console.log("Error: " + error);
            })
        }

    }

0 个答案:

没有答案