jQuery的岗位空,而不是JSON来的ASP.NET Web API [英] jQuery posts null instead of JSON to ASP.NET Web API

查看:130
本文介绍了jQuery的岗位空,而不是JSON来的ASP.NET Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法得到这个工作...我有一些jQuery像这样在客户端上:

I can't seem to get this to work... I have some jQuery like this on the client:

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

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

And in my Web.API controller I have a method like this:

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

我只是检查,看是否有文件住在服务器上,并返回一个布尔其是否确实与否。该报告字符串我送是UNC路径,所以reportpath看起来像'\\\\一些\\路径\\'。

I'm just checking to see if a file lives on the server, and to return a bool as to whether it does or not. The report string I am sending is a UNC path, so reportpath looks like '\\some\path\'.

我可以解雇剧本好,并创下在我ReportExists方法中设置断点,但报告变量总是空。

I can fire the script okay, and hit a breakpoint in my ReportExists method, but the report variable is always null.

我在做什么错了?

我也看到了一种用后.post的和postJSON。也许我应该使用其中的一个?如果是这样,就我的格式是什么?

I also see a way to post with .post and postJSON. Maybe I should be using one of those? If so, what would my format be?

更新:另外一个线索maybe-如果我删除[FromBody]然后我的断点清一色犯规被打到'没有HTTP资源发现,请求匹配。例如我在看显示,[FromBody]不需要......?

Update: An additional clue maybe- if I remove [FromBody] then my breakpoint doesnt get hit at all- 'No http resource was found that matches the request'. Examples I am looking at show that [FromBody] isn't needed...?

推荐答案

于是我发现了这个问题,并且解决方案。所以,第一件事情第一。根据contentType不能'应用程序/ JSON',它必须是空白(默认为应用程序/ x-WWW的形式urlen codeD我相信)。虽然它似乎你必须发送JSON,但没有在名称值对的名称。透过JSON.stringify也弄乱此设置。所以全工作jQuery的code是这样的:

So I found the problem, and the solution. So, first thing first. The contentType cannot be 'application/json', it has to be blank (default to application/x-www-form-urlencoded I believe). Although it seems you have to SEND json, but without a name in the name value pair. Using JSON.stringify also messes this up. So the full working jQuery code is like this:

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

在Web.API端,您必须对参数[FromBody] attibute,但比这的pretty标准等。 (对我来说)真正的问题是后。

On the Web.API side, you MUST have the [FromBody] attibute on the parameter, but other than this it's pretty standard. The real problem (for me) was the post.

在提琴手,请求身体看起来像这样=%5C%5Croot%5Cdata%5Creport.html

In Fiddler, the request body looked like this "=%5C%5Croot%5Cdata%5Creport.html"

后真的有答案,并与<一个href=\"http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1#sending_simple_types\">this文章这也是非常有益的。

This post really had the answer, and linked to this article which was also very helpful.

这篇关于jQuery的岗位空,而不是JSON来的ASP.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆