我有一个简单的actionmethod,返回一些json.它在ajax.example.com上运行.我需要从另一个站点someothersite.com访问它.
如果我试着打电话给我,我会得到预期的......:
Origin http://someothersite.com is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我知道有两种解决方法:JSONP和创建自定义HttpHandler来设置标头.
有没有更简单的方法?
简单的动作是否不可能定义允许的起源列表 - 或者简单地允许每个人?也许一个动作过滤器?
最佳将是......:
return json(mydata, JsonBehaviour.IDontCareWhoAccessesMe);
Run Code Online (Sandbox Code Playgroud) 当我尝试使用返回字符串的HttpClient调用 API 时,调用了subscribe 方法的错误处理程序。
前端代码:
this.httpClient.get<string>(`${this.baseUrl}/account/getname`).subscribe(
(accountName)=> console.log(accountName),
() => console.log("Failure")
);
Run Code Online (Sandbox Code Playgroud)
控制器中的后端代码:
[HttpGet]
public string GetName()
{
return "Sample Account Name";
}
Run Code Online (Sandbox Code Playgroud)
前端代码会将“ Failure”记录到控制台。
如果从 中删除错误通知方法subscribe(),则会发生以下错误
未捕获的类型错误:无法在 getOriginalError 处读取未定义的属性“ngOriginalError”