Second, small test skill--the first Ajax program __ajax

Source: Internet
Author: User
Tags stack trace

One, a new Ajax_start website

Add a Web service to your Web site (ws_start.asmx)
In vs2008, adding a Web waiter becomes two files, one is the asmx file, the other is the CS file in App_Code; I think it's not good, it's not easy to manage, at least now it seems to me, so we're going to try to reinvent it.

Iii. transforming the Web services generated in vs2008

1 first copy the code in the Ws_start.cs in App_Code and put it into the Ws_start.asmx file

2 Delete the Ws_start.cs file in App_Code

3 Remove the Codebehind attribute in the topmost webservice in the ws_start.asmx, as we have deleted the file associated with Codebehind in the previous step

4 because the CS file for the VS2008 generated Web service does not have a namespace, you must add a namespace to it: namespace Ajax_start

5 Modify the Class attribute value in WebService, because we have added a namespace to the generated class, so class= "Ajax_start.ws_start"

Add a Scriptmanage control to the Default.aspx page and associate it with the Web service you established above, the code is as follows:

<asp:scriptmanager id= "ScriptManager1" runat= "Server" >

<Services>

<asp:servicereference path= "~/ws_start.asmx"/>

</Services>

</asp:ScriptManager>

Five, write the function in the Web service, the new function is as follows:

Remove//[System.Web.Script.Services.ScriptService] comments before adding a function, because only classes with ScriptService can be invoked on the client via Ajax

[WebMethod]

public string Freshtime () {return DateTime.Now.ToString ();}

[WebMethod]

public string Hellosomebody (string name) {return "Hello:" + Name;}

[WebMethod]

public void ThrowError () {throw new Exception ("This is my thrown exception");}

Write the JS code to invoke the Web service, as follows:

<script type= "Text/javascript" >

function Getfreshtime () {Ajax_start.ws_start. Freshtime (whensuccess,whenfailed);}

function Gethellosomebody (value) {Ajax_start.ws_start. Hellosomebody (value,whensuccess,whenfailed);}

function Getthrowerror () {Ajax_start.ws_start. ThrowError (whensuccess,whenfailed);}

function whensuccess (Result) {

alert (result);

}

function whenfailed (Result) {

var str = "Type of server-side exception:" + result.get_stacktrace () + "/r/n"

+ "Exception description information:" + result.get_message () + "/r/n"

+ "The status code of the HTTP response that caused the exception:" + result.get_statuscode () + "/r/n"

+ "stack trace information for server-side exceptions:" + result.get_exceptiontype () + "/r/n"

+ "Whether it is due to network connection timeout:" + result.get_timedout ();

alert (str);

}

</script>

Write the page code that calls JS

<a onclick= "Getfreshtime ()" > Click to invoke Freashtime function </a>

<br/>

<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>

<a onclick= "Gethellosomebody (document.getElementById (' TextBox1 '). Value)" > Click Invoke hellosomebody function </a>

<br/>

<a onclick= "Getthrowerror ()" > Click to invoke ThrowError function </a>

Viii. test Results

The page is not refreshed, the Soldierluo is transferred to the server, and gets the result returned from the server Helo:soldierluo, OK, the first AJAX program is complete, then will simply introduce the important ASP.net AJAX control

 

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.