2015-09-16 49 views
0

ASPX頁面jQuery的AJAX方法不使用的WebMethod

<script> 
$(document).ready(function() { 
    $.ajax({ 
      type: "POST", 
      url: "WebForm1.aspx/GetData", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (response) { 
       $("#Content").text(response.d); 
      }, 
      failure: function (response) { 
       alert(response.d); 
      } 
     }); 
    }); 
</script> 
</head> 
<body> 
<form id="frm" method="post"> 
    <div id="Content"> 
    </div> 
</form> 
</body> 
</html> 

代碼背後

public static string GetData() 
{ 
    return "This string is from Code behind"; 
} 

我想用ajax這個功能,而無需使用 「WEBMETHOD」。即GetData()方法,我想在我的.aspx頁面中顯示而不使用Web服務。

+0

你需要WebMethod屬性。 – Zaki

回答

0

我不知道我明白你的問題,但也許你正在尋找的僅僅是你在onload活動頁面需要的代碼:

protected void Page_Load(object sender, EventArgs e) 
    { 
     Response.ContentType = "application/json"; 
     Response.Write("put a valid json string here"); 
    }