簡體   English   中英

如何將 curl 轉換為 javascript 發布請求

[英]how to convert curl to javascript post request

如何將此 curl 代碼轉換為適用於所有瀏覽器的 javascript 發布請求

curl https://connect.stripe.com/oauth/token \
-d client_secret=sk_test_f7PKXx5NRBFG5r41nTrPT7qB \
-d code="{AUTHORIZATION_CODE}" \
-d grant_type=authorization_code

在對這個答案Convert command curl to javascript進行一些研究和審查后,我發現我可以實現請求

$.ajax({
 url: "https://connect.stripe.com/oauth/token",
beforeSend: function(xhr) { 
xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password")); 
},
type: 'POST',
dataType: 'json',
contentType: 'application/json',
processData: false,

success: function (data) {
console.log(JSON.stringify(data));
},
  error: function(){
 console.log(error)
} 
});

如何添加 client_secret、code 和 grant_type?

它並不完全清楚你在要求什么。 但這里有一些片段來發出這個帖子請求
查詢

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://connect.stripe.com/oauth/token",
  "method": "POST",
  "headers": {
    "content-type": "application/x-www-form-urlencoded",
    "cache-control": "no-cache",
  },
  "data": {
    "client_secret": "sk_test_f7PKXx5NRBFG5r41nTrPT7qB",
    "code": "\"{AUTHORIZATION_CODE}\"",
    "grant_type": "authorization_code"
  }
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

vanialla JavaScript

var data = "client_secret=sk_test_f7PKXx5NRBFG5r41nTrPT7qB&code=%22%7BAUTHORIZATION_CODE%7D%22&grant_type=authorization_code";

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://connect.stripe.com/oauth/token");
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM