Jquery $. Ajax calls WebService and ashx

Source: Internet
Author: User

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"> 
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.