How to invoke the server-side method with JS. First, the format of the server-side method is as follows
[System.Web.Services.WebMethod]
PublicStaticvoid Servermethod (string url)
{
WebClient WC = new WebClient ();
StringBuilder postdata = new StringBuilder ();
Postdata.append ("multigateway=" + m_username);
Here is the GB2312 code
byte[] SendData = encoding.getencoding ("GB2312"). GetBytes (Postdata.tostring ());
Wc. Headers.add ("Content-type", "application/x-www-form-urlencoded");
Wc. Headers.add ("ContentLength", sendData.Length.ToString ());
byte[] Recdata = WC. Uploaddata (URL, "POST", senddata);
}
It is important to mark Red, which is the mandatory requirement of the server method, so that it can be called directly by the client.
Customer Point JS is called as follows
<script type= "Text/javascript" >
function Callservermethod (para) {
Pagemethods.setmultigatwaymessage (para,onsuccess);
}
function onsuccess (callbackvalue)//Call back after the backend method, get the parameters returned by the background
{
}
</script>
Don't forget to add ScriptManager and set the Enablepagemethods property to true on the last front page
<body>
<form id= "Form1" runat= "Server" >
<asp:scriptmanager id= "ScriptManager1" runat= "Server" enablepagemethods= "true" ></asp: Scriptmanager>
</div>
</form>
</body>
ASP. NET JavaScript client Invoke server-side method