In asp.net, ajax + WebService + json of jquery is used to implement the Code _ jquery

Source: Internet
Author: User
In asp.net, jquery ajax + WebService + json is used to implement the Code without refreshing the background value. This is better than page refreshing and provides a better user experience. For more information, see. First, paste Jquery's ajax:

The Code is as follows:


$. Ajax ({
Url: 'ws _ Ajax. asmx/BindDictByUpper ',
Type: 'post ',
ContentType: 'application/json; charset = UTF-8 ',
DataType: 'json ',
Data: '{PpareId: "' + varlue + '"}',
Success: function (data ){
Var dataObj = eval ("(" + data + ")");
// Response ('{mylist'{.html ('');
// For (var I = 0; I <msg. d. length; I ++ ){
// $ ('# Mylist'). append ('

  • '+ Msg. d [I] +'
  • ');
    //}
    Alert (dataObj );
    $. Each ({name: "John", lang: "JS"}, function (I, n ){
    Alert ("Name:" + I + ", Value:" + n );
    });
    $. Each (dataObj, function (idx, item ){
    If (idx = 0 ){
    Return true; // returns the same value as countinue, and returns the same value as break.
    }
    Alert ("name:" + item ['name'] + ", value:" + item ['value']);
    });
    },
    // Data: '{query: "' + $ ('# editquery'). val () + '"}',
    ProcessData: false
    });


    Well, write it at the front end, create a webservice page, and write the following method:

    The Code is as follows:


    ///


    /// Datatable converted to Json
    ///
    /// Datatable object
    /// Json string
    Public static string ToJson (DataTable Adt)
    {
    StringBuilder jsonString = new StringBuilder ();
    JsonString. Append ("[");
    Foreach (DataRow pdr in Adt. Rows)
    {
    JsonString. Append ("{");
    JsonString. appendFormat ("name: \" {0} \ ", value: \" {1} \ "", pdr ["dictionary content"]. toString (), pdr ["no."]. toString ());
    JsonString. Append ("},");
    // JsonString. appendFormat ("{name: '{0}', value: '{1}'},", pdr ["dictionary content"]. toString (), pdr ["no."]. toString ());
    }
    JsonString. Remove (jsonString. Length-1, 1 );
    JsonString. Append ("]");
    Return jsonString. ToString ();
    }


    And then try to run, this is the problem column, no matter how the operation can not be adjusted to the background method:
    Last, I checked the website for a long time.
    It was found that the new webservice page missing a key sentence:

    The Code is as follows:


    [System. Web. Script. Services. ScriptService]



    Add this sentence before the class.
    After that, run it again. OK, you can transfer it to the background.
    The background data is also sent to the foreground
    However, when the column is faulty, The sent data cannot be parsed into json data.
    As I said on the internet, you only need to set the dataType in ajax to json, but it actually does not work. Then I checked it online:
    1. For the JSON string returned by the server, if jquery asynchronous requests do not provide a type description or are accepted as strings, an object processing is required. The method is not too troublesome, is to put this string in eval () for execution once
    2. for the JSON string returned by the server, if the jquery asynchronous request sets the type (usually this Configuration Attribute) to "json", or uses $. the getJSON () method does not need the eval () method to obtain the server response, because the result is already a json object. You only need to call this object directly. Here, $. the getJSON method is used as an example to describe the data processing method.
    But I set his ype to json. Should I use le directly?
    So I put the transmitted data in the eval () method for processing, and then call it to le?
    In this way, the data obtained from the front-end can be processed as needed.
    I have another question:
    Why can't I find the data in this way (I am not very familiar with json, I used it for the first time ):

    The Code is as follows:


    $. Each (data. root, function (idx, item ){
    If (idx = 0 ){
    Return true; // returns the same value as countinue, and returns the same value as break.
    }
    Alert ("name:" + item. name + ", value:" + item. value );
    });
    });


    You can call the json data on the webpage ......
    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.