1. WebService. asmx
Process business data and generate weather data in the GetWhether Method for JqueryRequest. aspx to call. The Code is as follows:
Copy codeThe 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:
Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </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>