我想向 API 發送請求,資訊必須在正文中,我想在其中使用一個變數。但是當我使用(size)或{size}或$ {size}它不作業。如何在那里加載變數:
"body": "[\"variables\":{\"addToCartInput\":{\"productId\":\ ** SIZE ** \,\"clientMutationId\":\"addToCartMutation\"}}]",
var SIZE = "NI115N001-A130045000";
fetch("https://www.zalando.pl/api/graphql/add-to-cart/", {
"headers": {
"accept": "*/*",
"accept-language": "pl-PL,pl;q=0.9",
"content-type": "application/json",
"x-zalando-feature": "pdp",
},
"referrerPolicy": "strict-origin-when-cross-origin",
"body": "[\"variables\":{\"addToCartInput\":{\"productId\":\ ** SIZE ** \,\"clientMutationId\":\"addToCartMutation\"}}]",
"method": "POST"
});
uj5u.com熱心網友回復:
當有一種方法可以對陣列和/或物件進行字串化時,為什么還要費心轉義、串聯或插值(模板文字)?
var SIZE = "NI115N001-A130045000";
// Prepare the data
var data_to_send = [
variables:{
addToCartInput:{
productId: SIZE, // The variable is used here
clientMutationId: "addToCartMutation" // Assuming that is a string
}
}
];
// Stringify the data
var data_stringified = JSON.stringify(data_to_send);
fetch("https://www.zalando.pl/api/graphql/add-to-cart/", {
"headers": {
"accept": "*/*",
"accept-language": "pl-PL,pl;q=0.9",
"content-type": "application/json",
"x-zalando-feature": "pdp",
},
"referrerPolicy": "strict-origin-when-cross-origin",
"body": data_stringified, // Use the stringified data here
"method": "POST"
});
在有類似例子的地方獲取檔案
uj5u.com熱心網友回復:
這是兩種方法來做到這一點
1.
"[\"variables\":{\"addToCartInput\":{\"productId\": " SIZE " ,\"clientMutationId\":\"addToCartMutation\"}}]"
`["variables":{"addToCartInput":{"productId": ${SIZE},"clientMutationId":"addToCartMutation"}}]`
請注意,第一個使用雙引號。而第二個是使用模板文字。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/374198.html
標籤:javascript 邮政 拿来
