繁体   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