简体   繁体   English

发布请求从本地主机返回 401 但从邮递员工作

[英]Post request returns 401 from localhost but works from postman

I am trying to send API calls via POST-Request using th JavaScript fetch-API and Basic Authorization.我正在尝试使用 JavaScript fetch-API 和基本授权通过 POST-Request 发送 API 调用。 The request works from Postman, so the URL is correct.该请求来自 Postman,因此 URL 是正确的。 However, sending the request from localhost using VS Code Live Server Extension, the request returns "Failed to load resource: the server responded with a status of 401 ()".但是,使用 VS Code Live Server Extension 从本地主机发送请求时,请求返回“无法加载资源:服务器响应状态为 401 ()”。

Sending the same request from localhost to a postman mock-server also works.将相同的请求从本地主机发送到邮递员模拟服务器也可以。

This is my HTML file:这是我的 HTML 文件:

<!DOCTYPE html>

<html>
<head>
  <title>Title</title>
</head>

<body>
    <input id="name" type="text" name="name" placeholder="name">
    <input type="text" id="username" name="username">
    <input type="password" id="password" name="password">
    <button type="submit" onclick=JSONTest()> Submit </button>


  <script type="text/javascript">
    JSONTest = function() {
        var name = document.getElementById("name").value;
        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;
        
        let data = {title: name};
       

    fetch("url", {
        mode: 'no-cors',
        method: "POST",
        headers: {'Content-Type': 'application/json', 'Authorization': 'Basic ' + btoa(username + ":" + password)}, 
        body: JSON.stringify(data)
        }).then(res => {
  console.log("Request complete! response:", res);
});
    };
  </script>

</body>

</html>

Based on the information you have provided I am inclined to say it is likely a CORS issue.根据您提供的信息,我倾向于说这可能是CORS问题。 Without more information about the url you are trying to access it is hard to say what exactly the issue may be.如果没有关于您尝试访问的网址的更多信息,很难说出问题到底是什么。

It could also have to do with the protocol being used, make sure that you are using HTTPS if it is required.它还可能与正在使用的协议有关,如果需要,请确保您使用的是 HTTPS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将请求发布到 SPRING 引导应用程序无法从 Jquery(前端)工作,但从 POSTMAN 可以正常工作 - Post request to SPRING BOOT APPLICATION not working from Jquery(front-end) but works perfectly from POSTMAN 发布请求在 Postman 中有效,但在 Javascript 中无效 - Post Request Works in Postman But Not in Javascript 从发布请求接收 404 错误:postman - receive 404 error from post request: postman Sendgrid与SAPUI5应用程序的集成在POST上返回“ 400 Bad Request”,但在Postman上有效 - Sendgrid integration with SAPUI5 app returns '400 Bad Request' on POST but works on Postman 发布请求适用于邮递员,但不适用于javascript提取功能 - Post request works in postman but not in javascript fetch function post 请求在 Postman 和 cURL 中有效,但在 Angular 中无效 - The post request works in Postman and in cURL but not in Angular POST请求在POSTMAN中有效,但在我的代码JS中无效 - POST request works in POSTMAN but not in my code JS POST请求适用于Postman,但不适用于axios或.fetch() - POST request works with Postman, but not with axios or .fetch() POST请求因XMLHttpRequest()而失败,但可与Postman一起使用 - POST request fails with XMLHttpRequest(), yet works with Postman 简单的 POST 请求在 Postman 中有效,但在浏览器中无效 - Simple POST request works in Postman but not in browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM