1

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

3
  • And what does canvas.toDataURL() return, are you sure it's not null? insert a console.log(canvasData) before your ajax-request, what's logged in the browser console? Commented Jun 4, 2013 at 15:14
  • 1
    I think you may need to add dataType: "json" to your ajax call. Commented Jun 4, 2013 at 15:17
  • I added dataType: "json" and It is still not working :(. @soderslatt yeah, I am debugging with chrome and it is not null. I can see the base64 string Commented Jun 4, 2013 at 19:31

1 Answer 1

3

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);
                }
            });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.