繁体   English   中英

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

[英]jQuery posts null instead of JSON to ASP.NET Web API

我似乎无法让它工作......我在客户端上有一些像这样的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看起来像'\\\\ some \\ path \\'。

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

我究竟做错了什么?

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

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

所以我发现了问题和解决方案。 所以,首先要做的事情。 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方面,你必须在参数上有[FromBody]属性,但除此之外它是非常标准的。 真正的问题(对我来说)是帖子。

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

这篇文章真的有了答案,并且链接到这篇文章也很有帮助。

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;
        }
    }
});

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

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;
    }

如果您正在使用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中的数据对象与控制器的参数名称完全匹配。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM