簡體   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