我正在尝试创建一个在MVC 5中使用跨源请求(CORS)的Web应用程序.我已经尝试了所有内容而没有任何结果.
带有属性
public class AllowCrossSiteJsonAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
使用EnableCors属性
[EnableCors("*")]
Run Code Online (Sandbox Code Playgroud)
什么都行不通我开始认为这是不可能的
我知道这类问题在这里和一些论坛上已经问过好几次了。
我想做的是将文件上传到我的服务器。下面是我第一次尝试将标题设置为multipart/form-data. 以下是我的文件上传服务。
uploadNewFile (formData): Observable<any> {
const headers = new HttpHeaders();
headers.set('Content-Type', undefined);
return this.http.post(environment.baseURL+'api/v1/company/someFileUpload' , formData, {headers: headers})
.pipe(
catchError(this.formatErrors)
);
}
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我收到了这个错误。
错误:多部分:未找到边界
但是在看到这个问题( Send multipart/form-data files with Angular using $http )之后,我将标题更改为这个。
const headers = new HttpHeaders();
headers.set('Content-Type', undefined)
Run Code Online (Sandbox Code Playgroud)
但是当我这样改变它时,我收到了这个错误。
但是当我检查该错误的答案时,它说我需要在此处添加一个处理程序(Uncaught TypeError: Cannot read property 'ngOriginalError' of undefined at getOriginalError - 当 httpClient returned string)
但我有一个为我服务的处理程序。服务粘贴在下面。
uploadNewFile (formData): Observable<any> {
const headers = new HttpHeaders();
headers.set('Content-Type', undefined);
return this.http.post(environment.baseURL+'someFileUpload' , formData, {headers: headers}) …Run Code Online (Sandbox Code Playgroud)