簡體   English   中英

使用jQuery直接調用ASP.NET AJAX頁面方法 - 500(內部服務器錯誤)

[英]Using jQuery to directly call ASP.NET AJAX page methods — 500 (Internal Server Error)

我正在通過ajax和...學習asp方法

所以我試着去做ff。 來自這個網站: http//encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

在JS文件中:

 $(document).ready(function () {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function () {
    $.ajax({
        type: "POST",
        url: "WebForm1.aspx/GetDate",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // Replace the div's content with the page method's return.
            $("#Result").text(msg.d);
        }
    });
});

});

在aspx中:

 <html>
<head>
<title>Calling a page method with jQuery</title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="Scripts/JScript1.js"></script>
 </head>
  <body>
<div id="Result">Click here for the time.</div>
 </body>
 </html>

在代碼隱藏中:

namespace WebApplication1
 {
   public partial class WebForm1 : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string GetDate()
    {
        return DateTime.Now.ToString();
    }

}

}

當我在我的本地PC上的VS工作室上運行時,它工作得很好; 但是,一旦文件部署在IIS服務器中,當我單擊容器時會出現此錯誤:

POST http:///WebForm1.aspx/GetDate 500(內部服務器錯誤)

在IIS中有配置嗎? 或者是代碼相關的錯誤?

嘗試刪除此行[ScriptMethod(UseHttpGet = true)]使用此行當您嘗試獲取請求時,當您看到示例時,他們正在嘗試執行POST請求,並且不需要此行。

[WebMethod]
    public static string GetDate()
    {
        return DateTime.Now.ToString();
    }

您正在對GET方法發出POST請求,請嘗試將您的javascript更改為:

$("#Result").click(function () {
    $.get("WebForm1.aspx/GetDate", function (msg) {
        // Replace the div's content with the page method's return.
        $("#Result").text(msg.d);
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM