javascript - 多个 axios 请求
问题描述
我目前正在开展一个项目,我正在将结帐页面与 Klarna 集成。集成从 axios POST 请求(创建订单)开始。这个 POST 请求给了我一个响应,其中包括一个 Iframe html_snippet 和一个订单 ID。在这个发布请求之后,我应该向 Klarna 的其余 API 发送一个 GET 请求(确认页面),其中来自早期 POST 请求的订单 ID 将位于 GET 请求的 URL 中。问题是我不知道如何链接这些请求以便能够使用response.data.order_id
GET 请求 URL 中的 POST 请求。到目前为止,这是我的代码。axios.all()
显然不会工作,因为请求是同时发送的。
var axios = require("axios");
const { promiseImpl } = require("ejs");
const { response } = require("express");
var data = JSON.stringify({
purchase_country: "SE",
purchase_currency: "SEK",
locale: "sv-SE",
order_amount: 50000,
order_tax_amount: 4545,
order_lines: [
{
type: "physical",
reference: "19-402-USA",
name: "Red T-Shirt",
quantity: 5,
quantity_unit: "pcs",
unit_price: 10000,
tax_rate: 1000,
total_amount: 50000,
total_discount_amount: 0,
total_tax_amount: 4545,
},
],
merchant_urls: {
terms: "https://www.example.com/terms.html",
checkout: "https://atelierdecosmetique.herokuapp.com/checkout",
confirmation: "https://atelierdecosmetique.herokuapp.com/confirmation",
push: "https://www.example.com/api/push",
},
});
var config = {
method: "post",
url: "https://api.playground.klarna.com/checkout/v3/orders/",
headers: {
"Content-Type": "application/json",
Authorization:
"",
},
data: data,
};
//GET Request (read order)
var axios = require("axios");
var data1 = JSON.stringify({
purchase_country: "SE",
purchase_currency: "SEK",
locale: "sv-SE",
order_amount: 50000,
order_tax_amount: 4545,
order_lines: [
{
type: "physical",
reference: "19-402-USA",
name: "Red T-Shirt",
quantity: 5,
quantity_unit: "pcs",
unit_price: 10000,
tax_rate: 1000,
total_amount: 50000,
total_discount_amount: 0,
total_tax_amount: 4545,
},
],
merchant_urls: {
terms: "https://www.example.com/terms.html",
checkout: "https://www.example.com/checkout.html",
confirmation: "https://www.example.com/confirmation.html",
push: "https://www.example.com/api/push",
},
});
var config1 = {
method: "get",
url:
"https://api.playground.klarna.com/checkout/v3/orders/a94f3f40-df22-6334-95d8-eebb2cf8d986",
headers: {
"Content-Type": "application/json",
Authorization: "",
},
data: data1,
};
var Getrequest = axios(config1);
var Postrequest = axios(config)
axios.all([
Postrequest,
Getrequest
])
.then(function(response){
console.log(response[0].data.order_id)
app.get("/checkout", function(req, res){
res.render("checkout.ejs", {datapost: response[0].data.html_snippet})
})
app.get("/confirmation", function(req, res){
res.render("confirmation.ejs", {dataget: response[1].data.html_snippet})
})
})
.catch(function(err){
console.log(err)
})
我该怎么办?
解决方案
您可以将整个代码包装在一个async
函数中。然后你可以做类似的事情:
var Getrequest = await axios(config1);
var Postrequest = await axios(config);
// Do something with the data
await
停止整个功能,因此一切都会同步。
推荐阅读
- java - 在 Intellij Idea 中使用 hibernate 从实体类生成数据库表(不使用 Spring)
- haskell - GHC 的 -i 标志在命令行中有效,但在 OPTIONS_GHC 中无效
- c++ - 运算符中的条件=
- javascript - 有没有办法用 Axios 发布时刻(日期和时间)?将变量“startDateStr”连接到 https
- java - 设置 spring.jpa.open-in-view=false,然后 query.unwrap(NativeQueryImpl.class) 给出异常,为什么?
- ruby-on-rails - 将“命名空间”添加到序列化程序
- javascript - 单击按钮上的自定义 Javascript 确认窗口
- sql - 在非主键或唯一键上创建外键约束
- mysql - MySQL内部将浮点值四舍五入到小数点后一位
- powerbi - Power Bi DAX:显示过去 6 个月的数据