繁体   English   中英

出现错误:使用 AJAX POST 到 C# Webmethod 时出现 500 内部服务器错误

[英]Getting Error: 500 Internal server error when using AJAX POST to C# Webmethod

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

使用上述方法将我的 canvas 图像发布到 Web 方法,然后在下面的 c# 中进行简单检查。 我收到错误 500。我查看了各种帖子,但似乎找不到任何可以使其正常工作的调整,我已经关闭了 app_start 中的自动重定向和其他各种建议。 但还是一无所获。

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

WebMethod告诉应用程序通过它访问它和 XML WebService 请求。 如果你想通过POST访问它,你需要ScriptMethod属性:

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

有关更多信息,请参阅此答案:

web 服务中的 web 方法属性是什么?

我在谷歌浏览器中使用了开发者工具,点击了错误,然后在预览中..它告诉我 json 字符串长度太长。

我用以下内容编辑了 webconfig,它现在可以工作了!

<system.web.extensions> 
<scripting> 
<webServices> 
<jsonSerialization maxJsonLength="500000000"/>
 </webServices> 
</scripting> 
</system.web.extensions>

暂无
暂无

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

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