2015-09-16 48 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"); 
    }