gpt4 book ai didi

javascript - jQuery - 多个单独的 var formData = { POST 表单数据调用(我想逐一进行独立调用)

转载 作者:行者123 更新时间:2023-12-02 23:04:01 24 4
gpt4 key购买 nike

我想要一个一个地进行独立的调用,我想要一个一个地进行单独的表单数据作业,每个独立的调用并使用不同的数据,问题是进行了一次调用,这将是第一次调用:

var formData = { // First job
"Phone": "999999999"
};
$.post('http://localhost/', formData)
.done (showResult)
.fail(showError);
}
var formData = { // Second job
"Phone": "888888888"
};
$.post('http://localhost/', formData)
.done (showResult)
.fail(showError);

仅第一个作业成功。

最佳答案


/* By putting the data into an array, we achieve some important advantages:
*
* 1) We can use javascript's powerful array methods to manage the data.
* 2) We have abstracted the definition of the data away so that we could replace it with
* any other data source that returns a compatible array.
*
*/
const phones = ['999999999', '888888888']


/*
* Putting this function on its own allows for better maintainability by allowing it to call different
* urls with different data. Ideally, you would also supply success and error callbacks, but I didn't
* want to complicate the example.
*/
const doPost = (url, data) => {
$.post(url, data)
.done(res => {console.log(res})
.fail(err => {console.log(err})
}

/*
* Here I'm using one of the array methods that I mentioned earlier, Array.forEach. This
* function runs the supplied function once for each element in the array, so the first time
* phone==="999999999" and the second time phone==="888888888".
*
* Since it's called for each element, doPost will be called twice in this example.
*
* I used async/await to force the first function to complete before executing the next one.
* This might not actually be necessary, but since you say that your backend is "swallowing"
* the calls, I figured it was worth a try. Ultimately, though, you really want to find a way to do
* this more efficiently and in a non-blocking way.
*/
phones.forEach(async phone => await doPost('http://localhost/', {Phone: phone}))

关于javascript - jQuery - 多个单独的 var formData = { POST 表单数据调用(我想逐一进行独立调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57668308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com