我目前正在使用postman來發出一個推送正文資料的API請求。我可以使用 "x-www-form-urlencoded "或 "raw "來作業。 請看下面的例子:
我正在嘗試將我的圖片編碼為 "原始圖片"。
我正試圖將其轉換為ajax的javascript請求,但不確定如何格式化正文/資料文本。 以下是我的腳本:
$.ajax( {
type: 'POST'。
url: 'https://login.microsoftonline.com/***/oauth2/token'。
headers: {
"Content-Type": "application/json"。
},
data: {
" grant_type=client_credentials
&client_id=****
&client_secret=****
&資源=https://analysis.windows.net/powerbi/api"。
},
success。(data) => {
console.log(data.token)。
},
error: (data) => {
console.log('rr'/span>, data)
}
});
希望得到任何幫助
。uj5u.com熱心網友回復:
這里有一個不匹配,因為你將Content-Type頭設定為JSON,但你卻發送form-urlencoded。你需要一致地使用其中之一。
如果你想明確使用JSON,請這樣做:
如果你想明確使用JSON,請這樣做。
$.ajax( {
type: 'POST'。
url: 'https://login.microsoftonline.com/***/oauth2/token'。
contentType: 'application/json', //比直接設定頭檔案更短,但做同樣的事情。
data: JSON.stringify({
grant_type: 'client_credentials',
client_id: '***',
client_secret: '***', client_secret.
resource: 'https://analysis.windows.net/powerbi/api'
}),
success: data => {
console.log(data.token)
},
error: (xhr, textStatus, error) => {
console.log('rr'/span>, error)
}
});
如果你想使用一個form-url編碼的字串,這樣做:
$.ajax( {
type: 'POST'。
url: 'https://login.microsoftonline.com/***/oauth2/token'。
data: 'grant_type=client_credentials&client_id=***&client_secret=***&resourcehttps://analysis.windows.net/powerbi/api'。
success。data => {
console.log(data.token)
},
error: (xhr, textStatus, error) => {
console.log('rr'/span>, error)
}
});
注意在上面的例子中,error處理程式的第一個引數不是請求或回應資料,因為你的例子似乎期望這樣。我已經修改了這一部分,以接受正確的引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/323833.html
標籤:


