A simple example of a JQuery Ajax WebService pass parameter _jquery

Source: Internet
Author: User

When using jquery to implement Ajax in ASP.net, aspx,ashx can be used on the server side, as well as webservice. Recently studied the WebService mode, the jquery Ajax method calls the jquery Ajax method call asp.net WebService Detailed instance code this post gives a very detailed example, but does not give a detailed explanation. There are a few details of the problem began not to pay attention, the cost of the old to adjust the communication:

1) the key field name in the client data data must be strictly consistent with the server-side method parameters.

such as client:

 There is a parameter call
    $ (document). Ready (function () {
      $ ("#btn2"). Click (function () {
        $.ajax ({
          type: POST),
          ContentType: "Application/json",
          URL: "Webservice.asmx/getwish",
          data: "{value1: ' All things are done ', value2: ' Good luck ', Value3: ' Niu niu Niu, value4:2009} ',
          dataType: ' json ',
          success:function (Result) {
            $ (' #dictionary '). Append ( RESULT.D);}});
    

Server-side Getwish function arguments must be written as Value1,value2,value3,value4:

  [WebMethod]
  public string Getwish (string value1, String value2, string value3, int value4)
  {return
    string. Format ("Wish You in {3} years {0}, {1}, {2}", Value1, value2, Value3, value4);
  }

2 The client passes the object to the server-side method:

Client code:

 Incoming object
    $ (function () {
      $ (' #btn6 '). Click (function () {
        obj = new Object ();
        Obj.id = "1";
        Obj. Value = "AAA";
        ' {' obj ': {' ID ': ' 1 ', Value: ' Horse '}} '
        var d = ' {' obj ': ' + json.stringify (obj) + '} ';
        $.ajax ({
          type: Post),  //Access WebService use POST to request
          ContentType: "Application/json",//webservice Returns the JSON type
          URL: "Webservice.asmx/parmsobject",//Call WebService's address and method name combination----wsurl/method name
          data:d,     // Here is the parameter to pass, the format is data: "{Paraname:paravalue}", the following will see    
          dataType: ' json ',
          success:function (result) {
            alert (RESULT.D);
          },
          error:function (result) {
            alert ("fail");
          }
      );}

    );

Server-side code:

  [WebMethod]
  public string parmsobject (Class1 obj)
  {return

    obj.id + ': ' + obj. Value;
  }

Debugging Environment: Vs2010+jquery-1.3.2.min.js

The above is a small series for everyone to bring the jquery Ajax WebService transfer parameters of a simple example of all the content, I hope that we support the cloud-Habitat Community ~

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.