ServerStack TypeScript JsonServiceClient COR 问题

ServerStack TypeScript JsonServiceClient COR Issue

我在我的应用程序中使用 ServiceStack TypeScript 客户端 "JsonServiceClient"。单击按钮,我正在编写这些代码行。

let client:JsonServiceClient = new JsonServiceClient('http://ams-device-int.azurewebsites.net/');
client.setCredentials('testacc', 'Tes@2020');
client.get('/SourceDevice?Take=10').then((r) => {
  debugger;
});

执行此代码时,我在开发控制台中遇到了 CORS 问题。

Access to fetch at 'http://ams-device-int.azurewebsites.net/SourceDevice?Take=10' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

我已经与服务开发人员核实过,CORS 已经对所有来源启用,事实上,我们有一个 Web 应用程序 运行 AngularJS 代码,我在那里使用 $http,它工作正常,没有任何问题。我想我在这个 JsonServiceClient 中遗漏了一些东西,它没有发送正确的凭据,这是导致问题的原因。

另一方面,我有这个代码

const response = await fetch('http://ams-device-int.azurewebsites.net/SourceDevice?Take=10', {
    method: 'GET', // *GET, POST, PUT, DELETE, etc.
    //mode: 'same-origin', // no-cors, *cors, same-origin
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    //credentials: 'include', // include, *same-origin, omit
    headers: {
        'Content-Type': 'application/json',
        "Authorization": `Basic ${btoa('testacc:Tes@2020')}`
        // 'Content-Type': 'application/x-www-form-urlencoded',
    },
});
let res = await response.json();

这工作正常。请帮我解决我犯的错误。

此致, 贾米尔

听起来好像跨源服务器不支持 cookie,您可以更改模式以使用现有 ajax 请求所使用的模式,例如:

let client = new JsonServiceClient(baseUrl);
client.credentials = 'omit';
client.mode = ‘cors’;
client.headers.set("Authorization",`Basic ${btoa('testacc:Tes@2020')}`);