Jquery Ajax instance 3: send a request to WebService to call the method and return data

Source: Internet
Author: User

1. WebService. asmx

Process business data and generate weather data in the GetWhether Method for JqueryRequest. aspx to call. The Code is as follows:

[System. Web. Script. Services. ScriptService]
Public class WebService: System. Web. Services. WebService {

Public WebService (){

// If you use the designed component, uncomment the following line
// InitializeComponent ();
}

[WebMethod]
Public string GetWhether (string cityId)
{
Random r = new Random ();
Int degree = r. Next (100 );

String wInfo = string. Format ("Today {0}'s temperature is {1} degrees", cityId, degree );
Return wInfo;
}
}

Ii. AjaxRequest. aspx

Click the button to request the GetWhether (string cityId) method of WebService. asmx to obtain weather data. The Code is as follows:

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> bkjia.com </title>
<Script type = "text/javascript" language = "javascript" src = "js/jquery-1.3.2.js"> </script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input type = "text" name = "Text1" id = "Text1"/> <br/>
<Input type = "text" name = "Text2" id = "Text2"/>
<Br/>
<Input type = "button" id = "btn1" onclick = "BtnCity_Click ()" style = "width: 55px; height: 20px;"/>
</Div>
<Div id = "dd">
Sd
</Div>
<Div>
<Script type = "text/javascript" language = "javascript">
Function BtnCity_Click (){
Var city = $ ("# Text1"). val ();
$. Ajax ({
Url: "WebService. asmx/GetWhether ",
Data: {cityId: city },
Type: "post ",
Success: function (data, status ){
$ ("# Dd" 2.16.html ("}
});
}
</Script>
</Div>
</Form>
</Body>
</Html>

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.