Jquery Version 1.5.2
Pay attention to Ajax when calling WebService
[System. componentmodel. toolboxitem (false)] add this sentence
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
[System. Web. Script. Services. scriptservice] Remove this comment
WebService. asmx
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. services; //< summary> // Summary of WebService /// </Summary> [WebService (namespace = "http://tempuri.org/")] [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] [system. componentmodel. toolboxitem (false)] // to allow ASP. net Ajax calls this web service from the script. Please cancel the comments to the downstream. [System. web. script. services. scriptservice] public class WebService: system. web. services. webService {[webmethod] Public String helloworld () {return "Hello World";} [webmethod] Public String username (string name) {return "My name is:" + name. tostring ();} [webmethod] Public String userpwd (string PWD) {return "my password is:" + PWD. tostring ();}}
Handler. ashx
<% @ Webhandler Language = "C #" class = "handler" %> using system; using system. web; public class handler: ihttphandler {public void processrequest (httpcontext context) {context. response. contenttype = "text/plain"; if (context. request. form ["name"]! = NULL) {context. response. write (username (context. request. form ["name"]. tostring ();} If (context. request. form ["PWD"]! = NULL) {context. response. write (userpwd (context. request. form ["PWD"]. tostring () ;}} Public String username (string name) {return name;} Public String userpwd (string PWD) {return PWD ;} public bool isreusable {get {return false ;}}}
Default. aspx
Note that the data format must be
Data: "{PWD: '" + $. Trim ($ ("# userpwd"). Val () + "'}",
This is where I wrote the wrong code several times.
I often do not write this method when calling ashx. It is written as follows.
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">