Angular 收到 500 错误,但服务器正在顺利完成操作?

Angular receiving a 500 error but the server is completing the action with no issue?

我正在调用添加到数据库的 WebApi 方法。该方法成功并且没有抛出异常;但是 angular 出现 500 错误:

POST "MyServer" 500 (Internal Server Error)angular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:250 
(anonymous function)angular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:245 
mangular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:243 
fangular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:274 
(anonymous function)angular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:288 
k.$evalangular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:285 
k.$digestangular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:289 
k.$applyangular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:390 
(anonymous function)angular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:3 
n.event.dispatchangular?v=jZUm0Uxsk4Ig_MVBcIx2KTRnLbeaXFiNS47wjLSNXC01:3 r.handle   

所有这些都是内部 angular 代码,我不确定在哪里/如何调试这些东西

angular 服务

this.add = function (myObject) {
        $http({
            method: "POST",
            url: "api/theServer/Add/",
            data: myObject
        });
    };

WebApi

[Route("Add/"), HttpPost]
public void Add(MyObject myObject)
{
    _service.Add(waiver);
}

数据保存到数据库中很好,但是angular抛出错误并且没有更新页面

这是从服务器返回的:

The specified policy origin 'MyServer' is invalid. It must not contain a path, query, or fragment

更新

"Message": "An error has occurred.",
"ExceptionMessage": "The specified policy origin 'MyServer' is invalid. It must not contain a path, query, or fragment.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": "   at System.Web.Http.Cors.EnableCorsAttribute.ValidateOrigins(IList`1 origins)\r\n   at System.Web.Http.Cors.EnableCorsAttribute.GetCorsPolicyAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   at System.Web.Http.Cors.CorsMessageHandler.<GetCorsPolicyAsync>d__10.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Cors.CorsMessageHandler.<HandleCorsRequestAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Cors.CorsMessageHandler.<SendAsync>d__0.MoveNext()"

这是从 Postman 返回的完整堆栈

您的 Web 浏览器不允许从第三方站点获取信息。请求发送正常,但不允许 javascript 处理响应,因为它来自不受信任的来源。

您有多种选择,最常见的是:

  • rewrite proxy 添加到您的源服务器以转发数据包 到远程服务器
  • 在您的原始服务器上启用 CORS support 允许跨域请求

或者,您可能已经启用了 CORS,但您的 "MyServer" 主机不在允许的域列表中。