簡體   English   中英

Jquery REST API POST 調用,我收到 415 錯誤?

[英]Jquery REST API POST call, I'm getting 415 error?

我正在嘗試將進行 API 調用的 powershell 腳本之一翻譯成 javascript 或 ZD223E14391476E4782E2E1DCEEFFEE780E2F919Z 或 Achrome 擴展名。 我對 Jquery 不是很精通,所以在使用以下代碼(不受支持的媒體類型)撥打電話時出現錯誤 415。 我想我必須告訴它我正在使用“Content-Type”、“application/json”......但我沒有在我的 powershell 腳本中這樣做,直到我已經得到我的令牌之后的后續調用,所以我我有點困惑。 為什么我會收到 415 錯誤,我該如何解決?

var token
var userName = "userID"; 
var passWord = "PaSSwOrD"; 
var tokenURL = "https://55.55.55.55/api/v1/token";  
var xhr = new XMLHttpRequest();

var data =
'&grant_type=password';
'&server_host=55.55.55.55' +
'&username=' + userName +
'&password=' + passWord;
var dataFinal = encodeURI(data);

xhr.open('POST', tokenURL, true);
xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {
        //do something
    }
}
xhr.send(data);
alert(xhr.responseText);

我正在嘗試獲取用於調用 MOVEit 自動化 API: https://docs.ipswitch.com/MOVEit/Automation2021/API/REST-API的令牌

試試這個(如果我們談論 jQuery):

 var obj = null $.ajax({ url: "https://55.55.55.55/api/v1/token", method: 'POST', async: false, data: { grant_type: 'password', server_host: '55.55.55.55', username: 'userID', password: 'PaSSwOrD' }, headers: { 'Content-type': 'application/x-www-form-urlencoded', Accept:'application/json' }, success: function(data){ obj = data // here the result } }) console.log(obj) // here the result because of "async: false"

暫無
暫無

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

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