ASP. NET JavaScript client Invoke server-side method

Source: Internet
Author: User
Tags call back

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.