简体   繁体   English

401 未经授权 REST API 中闪电 web 组件 ZA8FF6C9514C35C8563B350382

[英]401 Unauthorized REST API in lightning web component salesforce

I'm trying to execute query using REST API, in a lightning web component.我正在尝试使用 REST API 在闪电 web 组件中执行查询。 the request in Postman returning result with success (enabling Follow Authorization header) but in the JavaScript in lightning web component it returns 401 Unauthorized the code in the java script is a follow: the request in Postman returning result with success (enabling Follow Authorization header) but in the JavaScript in lightning web component it returns 401 Unauthorized the code in the java script is a follow:

let sessionId = 'tokken';
let baseUrl = window.location.origin;
let header = {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "Authorization": "Bearer " + sessionId,
};
if (sessionId) {
    let options = {
        method: "GET",
        mode: 'no-cors',
        redirect: 'follow',
        headers: header,
    };
    fetch(baseUrl + '/services/data/v50.0/query/?q=SELECT+name+from+Account', options).then((response) => {
        console.log(JSON.stringify(response));
        if (!response.ok) {
            // throw Error(JSON.stringify(response));
        } else {
            return response.json();
        }
    }).then((repos) => {
        console.log(repos, repos);
    });
}

am I missing something?我错过了什么吗?

You can not send Authorization header with "no-cors" mode.您不能使用“no-cors”模式发送授权 header。

mode: "no-cors" only allows a limited set of headers in the request: mode: "no-cors"只允许请求中的一组有限的标头:

Accept
Accept-Language
Content-Language
Content-Type with a value of application/x-www-form-urlencoded, multipart/form-data, or text/plain

Since you can not pass the value Authorization to no-cors mode, you will need to add CORS configuration in your SalesForce as safe endpoint where they let you make a call.由于您无法将值Authorization传递给no-cors模式,因此您需要在 SalesForce 中添加 CORS 配置作为安全端点,它们可以让您拨打电话。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM