【问题标题】:asp.net HttpPost returning errorasp.net HttpPost 返回错误
【发布时间】:2018-03-25 20:55:42
【问题描述】:

我使用 ASP.NET Web API 的 HttpPost 总是从 AJAX 请求返回错误。

我的控制器:

public class ContactController : ApiController
{
    [HttpPost]
    [EnableCors(origins: "http://localhost:52884", headers: "*", methods: "*")]
    public string Post(myData m)
    {
        return String.Format("{0} {1:d}", m.FirstName, m.LastName);
    }
}

我的班级:

public class myData 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

我的 HTML 请求:

    <html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>

<body>
    <script>
        $.ajax(
        {
            url: "http://localhost:52884/api/contact",
            type: "POST",
            dataType: 'json',
            data: { FirstName: "FName", LastName: "LName" },
            success: function (result) {
                alert(result);
            },
            error: function (xhr, status, p3, p4) {
                var err = "Error " + " " + status + " " + p3;
                if (xhr.responseText && xhr.responseText[0] == "{")
                    err = JSON.parse(xhr.responseText).message;
                alert(err);
            }
        });
    </script>
</body>

</html>

当我在浏览器上运行它时,我收到警告:错误错误。

有谁知道我做错了什么?

如果我使用 Postman 运行它,它可以工作。但我在 Ajax 请求中需要它,因为其他网站会调用我的 API

【问题讨论】:

  • 首先,URL 应该是 /api/contact,其次是在模型中传递名称为 myData 的数据
  • 谢谢。是的,模型中的数据是 myData。我这里贴错了。我把网址改成了/api/contact,还是一样的问题
  • 我不知道为什么,但是我将 jsonp 添加到了我的 dataType 中,它给了我错误:Loading failed for the

标签: c# asp.net ajax asp.net-web-api


【解决方案1】:

返回错误,因为 ajax 无法解析 Post 方法响应

        [HttpPost]
    [EnableCors(origins: "http://localhost:{port}", headers: "*", methods: "*")]
    [Route("api/test")]
    public string Post(myData m)
    {
        return string.Format("{{ \"FirstName\" : \"{0}\", \"LastName\" : \"{1}\" }}", m.FirstName, m.LastName);
    }

【讨论】:

  • 这是 WeApi 而不是 MVC :)
  • 谢谢,还是同样的问题
【解决方案2】:

我只是用这种方式重构你的代码:

[HttpPost]
[EnableCors(origins: "http://localhost:{port}", headers: "*", methods: "*")]
[Route("api/test")]
public string Post(myData model)
{
    return String.Format("{0} {1:d}", model.FirstName, model.LastName);
}

public class myData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

和 Ajax 调用:

<script>
    model = {
        FirstName: "FName",
        LastName: "LName"
    }

    $.ajax({
        url: "api/test",
        type: "POST",
        dataType: 'json',
        data: model,
        success: function(result) {
            alert(result);
        },
        error: function(xhr, status, p3, p4) {
            var err = "Error " + " " + status + " " + p3;
            if (xhr.responseText && xhr.responseText[0] == "{")
                err = JSON.parse(xhr.responseText).message;
            alert(err);
        }
    });
</script>

希望你能从中得到一个想法!

【讨论】:

  • 我不知道为什么,但是我将 jsonp 添加到了我的 dataType 中,它给了我错误:Loading failed for the
  • @user6824563:你为什么用jsonp?
  • @user6824563:如果你在做Cross-Origin Requests,你必须在API中启用它,这不包含在答案中。
  • 如何在 API 中启用它?
  • @user6824563:请查看here
猜你喜欢
  • 1970-01-01
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 2017-12-09
  • 1970-01-01
相关资源
最近更新 更多