Skip to main content
 首页 » 编程设计

angularjs之Python flask 和AngularJS与链接http post请求顺序

2025年05月04日104虾米姐

我正在尝试将数据拆分成 block ,然后通过 AngularJS $http.post 发送到 Python flask 来电。在 JS 方面,似乎请求是按给定的顺序发出的。但在 Python 方面,请求似乎并没有按照预期的顺序到达。

临时代码如下所示:

JS:

var xdata = ["0", "1", "2", "3", "4", "***__END__***"].map(x => JSON.stringify(x)); 
var post_chain = []; 
for (var i = 0; i < xdata.length; i++) { 
    post_chain.push($http.post("/path", xdata[i])); 
} 
 
post_chain[0].then(function (response0) { 
    console.log("response0"); 
    return post_chain[1]; 
}).then(function (response1) { 
    console.log("response1"); 
    return post_chain[2]; 
}).then(function (response2) { 
    console.log("response2"); 
    return post_chain[3]; 
}).then(function (response3) { 
    console.log("response3"); 
    return post_chain[4]; 
}).then(function (response4) { 
    console.log("response4"); 
    return post_chain[5]; 
}).then(function (response) { 
    console.log('Done'); 
    // Handle response 
}); 

在 Python 中,我使用 flask.request.get_json(silent=False)获取输入数据。有一些代码可以检测终止标准(例如,由字符串 "***__END__***" 给出)表示来自 JS 的帖子结束。这就是为什么我需要确保从 JS 到 Python 的请求顺序。我还将收到的数据打印出来。

JS 中的控制台输出看起来不错(按给定顺序显示 0、1、2 等)。但是在 Python 中,打印的数据是乱序的(0、4、1 等)。

稍后,我打算用递归函数来概括 JS 代码。但现在,我试图保证 Python 代码以正确的顺序接收数据。

有什么建议么?

更新 1

建议有效!但是,理想的方法是将代码推广到任何数组大小。我认为这可以通过递归来完成,但它似乎并没有以正确的顺序传递数据。少了什么东西?
var post_chain_call = function(i, post_element) { 
    if (i == post_chain.length - 1) { 
        return post_element(xdata[i]).then(function (response) { 
            // Handle response 
        }); 
    } else { 
        return post_element(xdata[i]).then(post_chain_call(i + 1, post_chain[i + 1])); 
    } 
} 
post_chain_call(0, post_chain[0]); 

更新 2

让它与另一种方法一起工作!
var post_chain_call = function(i, post_element) { 
    if (i == post_chain.length - 1) { 
        return post_element.then(function (response) { 
            // Handle response 
        }); 
    } else { 
        return post_element.then(function (response_tmp) { 
            return post_chain_call(i + 1, post_chain[i + 1](xdata[i + 1])); 
        }); 
    } 
} 
post_chain_call(0, post_chain[0](xdata[0])); 

请您参考如下方法:

您的前端开始与后端通信的时刻就是您将它们分配给 postChain 的时刻。 .这意味着每个请求本身都在​​尝试与服务器通信,并且无法预测它们连接到服务器的顺序。确保请求仅在 .then() 中实例化的可能解决方法先前的请求是不存储 $http.post() 的结果调用链,而是执行 $http.post() 的函数并返回那个 promise 。

    var xdata = ["0", "1", "2", "3", "4", "***__END__***"].map(x => JSON.stringify(x)); 
    var post_chain = []; 
    for (var i = 0; i < xdata.length; i++) { 
        // this way, the code is not yet executed, but it will when you call it. 
        post_chain.push((data) => $http.post("/path", data)); 
    } 
 
    post_chain[0](xdata[0]).then(function (response0) { 
        console.log("response0"); 
        return post_chain[1](xdata[1]); 
    }).then(function (response1) { 
        console.log("response1"); 
        return post_chain[2](xdata[2]); 
    }).then(function (response2) { 
        console.log("response2"); 
        return post_chain[3](xdata[3]); 
    }).then(function (response3) { 
        console.log("response3"); 
        return post_chain[4](xdata[4]); 
    }).then(function (response4) { 
        console.log("response4"); 
        return post_chain[5](xdata[5]); 
    }).then(function (response) { 
        console.log('Done'); 
        // Handle response 
    }); 

您还可以使用箭头函数来确保 xdata仍在函数范围内,您不需要向它传递任何参数
    var post_chain = []; 
    for (var i = 0; i < xdata.length; i++) { 
        // this way, the code is not yet executed, but it will when you call it. 
        post_chain.push(() => $http.post("/path", xdata[i])); 
    } 
    post_chain[0]().then((response0) => { 
        console.log("response0"); 
        return post_chain[1](); 
    }).then((response1) => { 
        console.log("response1"); 
        return post_chain[2](); 
    }).then((response2) => { 
        console.log("response2"); 
        return post_chain[3](); 
    }).then((response3) => { 
        console.log("response3"); 
        return post_chain[4](); 
    }).then((response4) => { 
        console.log("response4"); 
        return post_chain[5](); 
    }).then((response) => { 
        console.log('Done'); 
        // Handle response 
    }); 

我希望这有帮助。