1
var image = document.getElementById("capture").toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');

alert(image);

        $.ajax({
            type: 'POST',
            url: 'Info.aspx/testingPOST',
            data: '{ "imageData" : "' + image + '" }',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(response, textStatus, jqXHR) {
                alert("File Saved");
            },
            error: function (jqXHR, exception) {
    var msg = 'error';
    if (jqXHR.status === 0) {
        msg = 'Not connect.\n Verify Network.';
    } else if (jqXHR.status == 404) {
        msg = 'Requested page not found. [404]';
    } else if (jqXHR.status == 500) {
        msg = 'Internal Server Error [500].';
    } else if (exception === 'parsererror') {
        msg = 'Requested JSON parse failed.';
    } else if (exception === 'timeout') {
        msg = 'Time out error.';
    } else if (exception === 'abort') {
        msg = 'Ajax request aborted.';
    } else {
        msg = 'Uncaught Error.\n' + jqXHR.responseText;
    }
    alert("error:" + msg);
    }
            })
        }

Using the above to post my canvas image to the Webmethod and then just a simple check in c# below. I am getting error 500. I have looked on various posts and can't seem to find any tweak that gets this working, I have turned off the auto-redirect in app_start and various other suggestions. But still nothing.

[WebMethod]
    public static bool testingPOST(string value)
    {
        
        return true;
    }
n00ms
  • 21
  • 1
  • 4
  • To start, you need to configure your server app to report the full details of the error so you have a better idea of what's wrong. – Jasen Nov 30 '21 at 00:23
  • try to change `data: '{ "imageData" : "' + image + '" }'` to `data: { value : image }`, – cura Nov 30 '21 at 00:29
  • 1
    The 500 error details are hidden by default for security reasons. It should be disabled for production servers. The details on how to do this varies depending on the app technology -- so you really need to search for a specific setup (and sometimes version). – Jasen Nov 30 '21 at 00:34
  • I have changed the suggestion by Cura but still no fix. – n00ms Nov 30 '21 at 10:00
  • I am currently using in the webconfig ``` ``` but only getting 500 error in the details in the browser – n00ms Nov 30 '21 at 10:00
  • I am running this via the debug mode in Visual Studio via IIS Express – n00ms Nov 30 '21 at 10:52
  • I used the developer tools in Google Chrome, and clicked on the error, then on preview.. it showed me that the json string length was too long. I had to add ``` ``` to my webconfig and it worked – n00ms Nov 30 '21 at 11:19

2 Answers2

1

I used the developer tools in Google Chrome, and clicked on the error, then on preview.. it showed me that the json string length was too long.

I edited the webconfig with the following and it now works!

<system.web.extensions> 
<scripting> 
<webServices> 
<jsonSerialization maxJsonLength="500000000"/>
 </webServices> 
</scripting> 
</system.web.extensions>
n00ms
  • 21
  • 1
  • 4
0

WebMethod tells the applicaiton it is accessed through and XML WebService request. if you want to access it through POST you will need the ScriptMethod attribute:

[ScriptMethod]
public static bool testingPOST(string value)
{
    return true;
}

See this answer for more info:

what is web method attribute in web service?

CorrieJanse
  • 2,374
  • 1
  • 6
  • 23