【问题标题】:Failed with 500 status - angular js以 500 状态失败 - angular js
【发布时间】:2015-01-14 16:33:58
【问题描述】:

我有 Angular js 和 web api 的项目。我的项目托管在 IIS6.2 中。根据一些链接,我已将 mime 类型 json 添加到 IIS。

当我试图在异常上抛出错误时,它工作得很好。但是当我成功返回响应对象时,它返回 500 状态。

实际上,在我托管后它在本地而不是在 IIS 中运行良好。

我试过了,

  • 不带accept动词
  • [AcceptVerbs("Post")]
  • [AcceptVerbs("Post","Get")]

当我从本地执行我的项目时,它执行完美,没有任何错误。 但是当我从服务器执行时,我得到了,

 Failed with 500 status

在这里我很困惑,因为数据保存得很好。但是出现错误失败,状态为 500。

从 web api,我得到了以下问题,

<Error>
<Message>
The requested resource does not support http method 'GET'.
</Message>
</Error>

代码: 网络接口

[AcceptVerbs("Post")]



public HttpResponseMessage Signup(RegisterModel registerModel)
        {
        HttpResponseMessage response;
        try
        {
            BLUser.UpdateUser(User);

            response = Request.CreateResponse(HttpStatusCode.OK) or Request.CreateResponse(HttpStatusCode.Created);
        }
        catch (Exception ex)
        {
            HttpResponseMessage response = new HttpResponseMessage();
                response.Content = new StringContent(errorMessage);
                response.ReasonPhrase = errorMessage;
                response.StatusCode = HttpStatusCode.BadRequest;

                throw new HttpResponseException(response);
        }

        return response;
    }

Angular js

$http({
                method: "post",
                url: "/api/Common/Signup",
                data: user
            });

你能提供解决这个问题的方法吗?

【问题讨论】:

  • 您正在返回异常 return new HttpResponseException(response);,但您期望 200 OK 状态?
  • 抱歉误提。我没有回来,我只是抛出异常。我已经编辑了这个问题。请再看看我的问题。对不起。抛出新的 HttpResponseException(response);
  • 您的问题与 AngularJS 无关,而 HTTP 错误 500 是内部服务器错误。
  • 这是@Sapher 所说的后端错误。但是,如果您使用的是 Chrome,这可能会对您有所帮助,请转到开发人员工具栏,然后单击网络选项卡以查看该错误可能是什么。您可以尝试使用不同的数据进行多次调查。
  • 但是在本地,我没有收到这个错误。这就是为什么困惑..

标签: angularjs iis-6 asp.net-web-api


【解决方案1】:

虽然这可能无法解决问题,但您似乎缺少响应中的“位置”标头。我不确定您的设置,但如果提供了新资源,浏览器通常会转到新资源的位置。

我认为你需要设置的属性是 response.Headers.Location

【讨论】:

    【解决方案2】:

    您是否使用 curl 或 postman 尝试过。在使用 Angular 进行测试之前,请尝试在 Angular 之前使用 Postman 对其进行测试。

    另外 data: user 用户对象属性必须与 RegisterModel 中的属性匹配,否则 .NET 将无法解析 json 并将其转换为将通过 ERROR 的类.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 2015-08-28
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多