简体   繁体   English

从Ajax到Web API执行HTTP POST的空参数

[英]Null parameters doing HTTP POST from Ajax to Web API

I've read several StackOverflow posts about this and corrected my code several times but I cant get my webAPI post method to work. 我已经阅读了几篇有关此问题的StackOverflow帖子,并多次更正了我的代码,但我无法使用我的webAPI帖子方法。 I'm trying to receive a post parameter but it comes always null. 我正在尝试接收一个post参数,但它始终为null。

What I am trying to do is receiving a base64 string that represents a canvas that is creating with jquery when the user clicks the button: 我要做的是接收一个base64字符串,表示当用户单击按钮时使用jquery创建的画布:

function MakePhoto(ctrl) {

    html2canvas(ctrl, {
        onrendered: function (canvas) {

            var canvasData = canvas.toDataURL()
            jQuery.ajax({
                url: "../api/webinfo",
                type: "POST",
                data: { imagedata: canvasData },
                success: function () {
                    alert("success");

                },
                error: function () {
                    alert("failure");
                }
            });

        }
    });

}

my WebInfoController.cs looks like this: 我的WebInfoController.cs看起来像这样:

 public void Post([FromBody]string imagedata)
 { 

 }

imagedata parameter comes always NULL imagedata参数始终为NULL

And this are the headers I am receiving at webapi: 这是我在webapi收到的标题:

"Method: POST, “方法:POST,

RequestUri: 'http://myhost/RestFulApi/api/webinfo'

Content: System.Net.Http.StreamContent 内容:System.Net.Http.StreamContent

User-Agent: Chrome/27.0.1453.94 用户代理:Chrome / 27.0.1453.94

Content-Length: 42226 内容长度:42226

Content-Type: application/x-www-form-urlencoded; 内容类型:application / x-www-form-urlencoded; charset=UTF-8} 字符集= UTF-8}

I hope you could help me. 我希望你能帮助我。

Thanks 谢谢

OK, I found the problem after some hours researching. 好的,经过几个小时的研究后我发现了问题。 I had to pass the data parameter to ajax function using: 我必须使用以下方法将data参数传递给ajax函数:

"=" + canvasdata, without the parameter name:

jQuery.ajax({
                url: "../api/webinfo",
                type: "POST",
                data: "=" + canvasData,
                success: function (response) {
                    alert(response);

                },
                error: function (response) {
                    alert(response.responseText);
                }
            });

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

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