Carlos Landeras
Carlos Landeras

Reputation: 11063

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. I'm trying to receive a post parameter but it comes always 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:

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:

 public void Post([FromBody]string imagedata)
 { 
            
 }

imagedata parameter comes always NULL

And this are the headers I am receiving at webapi:

"Method: POST,

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

Content: System.Net.Http.StreamContent

User-Agent: Chrome/27.0.1453.94

Content-Length: 42226

Content-Type: application/x-www-form-urlencoded; charset=UTF-8}

I hope you could help me.

Thanks

Upvotes: 1

Views: 2091

Answers (1)

Carlos Landeras
Carlos Landeras

Reputation: 11063

OK, I found the problem after some hours researching. I had to pass the data parameter to ajax function using:

"=" + 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);
                }
            });

Upvotes: 3

Related Questions