jquery invokes the WebService method of passing a JSON array _jquery

Source: Internet
Author: User

The example in this article describes the way jquery calls WebService to pass a JSON array. Share to everyone for your reference, specific as follows:

jquery makes it easy to invoke the WebService implementation asynchronously by providing a $.ajax powerful method, passing the JSON string to the WebService on the page, webservice the method for business processing, and returning the JSON object to the page, Let the page to show.

It's all very simple, and that's not the thing to learn today. In the actual processing of the business process, we will find that often the page to pass to WebService is not one or more strings, and sometimes need to pass a combination of data, such as a set of data:

Copy Code code as follows:
{' Employee ': [{' Name ': ' John ', ' sex ': ' Man ', ' age ': '},{' ' name ': ' Tom ', ' Sex ': ' Mans ', ' age ': ' 21 '}]}

It is no problem for the client to use such a JSON string as the data parameter of the $.ajax method, however, the webservice of the server to write the receive parameter is a problem. In Baidu, Google after some, only to find that the question did not answer. Let's just go to the research, find out in fact the employee object is an array first, and each item of its number group is a dictionary<string,string> dictionary type. So I try to use dictionary<string,string>[] employee in the server to receive the client pass parameters, everything as I expected, success!

The client code is as follows:

JQuery invokes WebService import data
function LoadData () {
    var studentdata = Collectiondata ();
    $.ajax ({
      URL: "Importdataservice.asmx/importstu",
      type: "Post",
      ContentType: "Application/json; Charset=utf-8 ",
      dataType:" JSON ",
      data:" {' Students ': [{' Name ': ' KoBe ', ' sex ': ' Boy ', ' age ': '},{' ": ' Mary ', ' Sex ': ' Girl ', ' age ': ' {'}]} ',
      success:function (result) {
        alert (RESULT.D);
      },
      error: Function (e) {
        alert (e.responsetext);}}
    );


The service-side code is as follows:

<summary>//////</summary>///<param name= "Students" ></param>///<returns></ returns> [WebMethod] [Scriptmethod (Responseformat=responseformat.json)] public string Importstu (Dictionary< string,string> []students) {if (students).
  Length = = 0) {return "no data!"; else {try {foreach (dictionary<string, string> stu in students) {//construct a new Studen
        T object.
        Student Student = new Student ();
        Assigns a value to the newly constructed student object property. foreach (String key in Stu. Keys) {switch (key) {case ' name ': Student.
              Name = Stu[key];
            Break Case "Sex": Student.
              Sex = Stu[key];
            Break
              Case ' age ': int age; if (Int32.TryParse (Stu[key], out of age)) {student.
              Age = age; else {student. Age = 0;
            } break;
          Default:break;
    }} return "Import student success!";
    Catch {throw new Exception ("Failed to import student!");

 }
  }
}

Note that the server-side parameter name needs to be the same as the key value of the client JSON array, as in the code above, the parameter names are students.

More interested readers of jquery-related content can view the site topics: "jquery Extended Tips," "jquery common Plug-ins and Usage summary", "jquery drag-and-drop effects and tips summary", "jquery table (table) Operation Tips Summary", " A summary of Ajax usage in jquery, a summary of common classic effects in jquery, a summary of jquery animation and special effects usage, and a summary of jquery selector usage

I hope this article will help you with the jquery program design.

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.