首页 文章

jQuery 将_而不是 JSON 发布到 ASP.NET Web API

提问于
浏览
15

我似乎无法让它工作......我在客户端上有一些像这样的 jQuery:

$.ajax({
    type: "POST",
    url: "api/report/reportexists/",
    data: JSON.stringify({ "report":reportpath }),
    success: function(exists) {
        if (exists) {
            fileExists = true;
        } else {
            fileExists = false;
        }
    }
});

在我的 Web.API 控制器中,我有一个这样的方法:

[HttpPost]
public bool ReportExists( [FromBody]string report )
{
    bool exists = File.Exists(report);
    return exists;
}

我只是检查一个文件是否存在于服务器上,并返回一个 bool 是否存在。我发送的报告字符串是 UNC 路径,因此 reportpath 看起来像'\ somepath '。

我可以解决脚本问题,并在 ReportExists 方法中点击断点,但报表变量始终为 null。

我究竟做错了什么?

我还看到了使用.post 和 postJSON 发布的方法。也许我应该使用其中之一?如果是这样,我的格式是什么?

**更新:**一个额外的线索可能 - 如果我删除[5]然后我的断点根本没有被击中 - '没有找到匹配请求的 http 资源'。我看的例子表明[6]不需要......?

5 回答

  • 25

    所以我发现了问题和解决方案。所以,首先要做的事情。 contentType 不能是'application/json',它必须是空白的(默认为 application/x-www-form-urlencoded 我相信)。虽然看起来你必须发送 json,但在名称值对中没有名称。使用 JSON.stringify 也会搞砸了。所以完整的 jQuery 代码是这样的:

    $.ajax({
        type: "POST",
        url: "api/slideid/reportexists",
        data: { "": reportpath },
        success: function(exists) {
            if (exists) {
                fileExists = true;
            } else {
                fileExists = false;
            }
        }
    });
    

    在 Web.API 方面,你必须在参数上有[3] attibute,但除此之外它是非常标准的。真正的问题(对我来说)是帖子。

    在 Fiddler 中,请求体看起来像这样“=%5C%5Croot%5Cdata%5Creport.html”

    这个帖子确实有答案,并链接到这个文章,这也很有帮助。

  • 13

    jQuery.ajax()默认情况下将 contentType 设置为application/x-www-form-urlencoded。您可以在application/json中发送请求。此外,您应该将数据作为字符串发送,它将获得模型绑定到 post 方法的report参数:

    $.ajax({
        type: "POST",
        url: "api/report/reportexists/",
        contentType:  "application/json",
        data: JSON.stringify(reportpath),
        success: function(exists) {
            if (exists) {
                fileExists = true;
            } else {
                fileExists = false;
            }
        }
    });
    
  • 6

    这对我有用,所有其他方法都没有:

    function addProduct() {
            var product = { 'Id': 12, 'Name': 'Maya', 'Category': 'newcat', 'Price': 1234 };             
            $.ajax({
                type: "POST",
                url: "../api/products",
                async: true,
                cache: false,
                type: 'POST',
                data: product,
                dataType: "json",
                 success: function (result) {
    
                },
                error: function (jqXHR, exception) {
                    alert(exception);
                }
            });
        }
    

    服务器端:

    [HttpPost]
        public Product[] AddNewProduct([FromBody]Product prod)
        {
            new List<Product>(products).Add(prod);
            return products;
        }
    
  • 0

    如果您正在使用 MVC 的FromBody属性,则 MVC 绑定器会将此视为可选参数。这意味着即使您只有一个FromBody参数,也需要明确参数名称。

    你应该可以使用这样简单的东西:

    控制器:

    [HttpPost]
    public bool ReportExists( [FromBody]string report )
    {
        bool exists = File.Exists(report);
        return exists;
    }
    

    Javascript

    $.ajax({
        type: "POST",
        url: "api/report/reportexists/",
        data: { "report":reportpath },
        success: function(exists) {
        ...
    

    您必须确保 jQuery 中的数据对象与控制器的参数名称完全匹配.

  • -1

    $.post 为我服务的目的。从 webapi 中删除[24]并在 jquery 客户端的$.post 的 url 参数中提供 url。有效!

相关问题