[英]Ajax calling 401 unauthorized from backend in html
I have a problem with my AJAX.我的 AJAX 有问题。 Using Insomnia, I was able to get in with a successful response of 200 using the API token.使用 Insomnia,我能够使用 API 令牌成功响应 200。
However, when I implement it in the HTML, I get a 401 response of access denied .但是,当我在 HTML 中实现它时,我得到了access denied的 401 响应。
$.ajax({ url: "https://ecoexchange.dscloud.me:8080/api/get", method: "GET", apikey: sessionStorage.getItem("apikey"), dataType: 'json', success: function(result) { $('#infoTable tr').empty(); var header = $('#infoTable thead'); var body = $('#infoTable tbody'); var hTr; $('#infoTable thead').append(hTr = $('<tr>')); // Headers for (var h = 0; h < result.headers.length; h++) { hTr.append($('<th>', { text: result.headers[h] })) } // Body for (var d in result.data) { var data = result.data[d]; $('#infoTable tbody').append($('<tr>').append($('<td>', { text: data.RecyclableID })).append($('<td>', { text: data.Name })).append($('<td>', { text: data.RecyclableType })) ) } } })
I am not sure how to put in the token or user name or password.我不确定如何输入令牌或用户名或密码。
How can I improve the code so I don't get the error?如何改进代码以免出现错误?
What is this apikey
parameter you're using?您使用的这个apikey
参数是什么? That's not in the documentation .那不在文档中。
apikey: sessionStorage.getItem("apikey"),
Did you mean to pass it as a header instead?您是否打算将其作为header传递? For example:例如:
headers: {"apikey": sessionStorage.getItem("apikey")},
The documentation for the service you're using should specify how to include the API key.您正在使用的服务的文档应指定如何包含 API 密钥。 Presumably you have that information, because:大概你有这些信息,因为:
Using Insomnia, I was able to get in with a successful response使用 Insomnia,我能够成功响应
So you'll need to include the value in your AJAX request wherever it belongs.因此,您需要在 AJAX 请求中包含该值,无论它属于何处。 Most likely as either a header value or a query string value.很可能是 header 值或查询字符串值。 But the jQuery .ajax()
function isn't going to know how to pass the value, you have to specify.但是 jQuery .ajax()
function 不知道如何传递值,您必须指定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.