Home > database >  Getting Error: 500 Internal server error when using AJAX POST to C# Webmethod
Getting Error: 500 Internal server error when using AJAX POST to C# Webmethod

Time:12-01

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;
    }

CodePudding user response:

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?

CodePudding user response:

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>
  • Related