Talking about the use of the summary of Jquery + JSON + WebService

https://www.jb51.net/article/36207.htm

 Updated: April 28, 2013 12:19:55 Author:     I want to comment
 
This article describes the use of the summary of Jquery + JSON + WebService. A friend in need refer to the following
 

JS Jquery as a good framework, easy to use features do not have to say. In the actual development process, the use of JQ's AJAX function calls WebService

The interface AJAX functionality has become a relatively common means of technology. Achieve WebService interface, usually by the OOP language. and so

In the WebService interface function, you may inevitably encounter complex data types in addition to simple data types. Complex data types machine data are likely to be

WebService interface parameters, there may be a return value of WebService. Important as we recited herein:

1, WebService interfaces for complex types of parameters when calling JQ incoming JSON data should be how to represent. ?

2, JQ get JSON data types WebService call.

3, JQ called What requirements for complex data types Webservice returned. ?

Environment: JQ Version: 1.4.2, VS2008 SP1.

Test 1: WebService for simple parameter types:

Copy the code code is as follows:

WebService interface function code is as follows: 

    [the WebMethod (the Description = "Test Methods")] 
    public String ProcessPersonalInfo (the Person Person) 
    { 
        return person.Name + person.Tel; 
    } 
    JQ calling code as follows: 

        $ .ajax ({ 

        type: "the POST" , 

        URL: "WebService1.asmx / the GetString", 

        dataType: "JSON", 

        contentType: "file application / JSON; charset = UTF-. 8", 

        Data: "{ 'name': 'zhangsan'}", 

        Success: function (JSON ) {Alert (json.d)}, 

        error: function (error) { 

        Alert ( "error call" + error.responseText);   

        } 
    });

Tip: $ .ajax function, data must be represented as a string of JSON, but can not pass directly into JSON data. Perhaps some friends to the JSON object and a string of JSON objects

 

Not good distinction, in fact, similar to a string in C # with '' caused to things, but JSON object is directly written in the {}. The test method is a simple pop up directly through the alert function, if the display [object: object]

Compared with JSON object, otherwise it is a string.

The results as shown below:

Test Two: For WebService complex parameter types:

Copy the code code is as follows:

WebService interface function code is as follows: 

        [the WebMethod (the Description = "Test Methods")] 
        public String ProcessPersonalInfo (the Person Person) 
        { 
            return person.Name + person.Tel; 
        } 

        the Person entity: 

        public class the Person 
        { 
            public String the Name {GET; SET; } 

            public int Age {GET; SET;} 

            public String the Address {GET; SET;} 

            public String {GET Tel; SET;} 

        } 

JQ calling code as follows: 

        $ .ajax ({ 

            type: "the POST", 

            URL: "the WebService1. asmx / ProcessPersonalInfo ", 

            dataType: "json", 

            contentType: "application/json; charset=utf-8", 

            data: "{'person':{'Name':'zhangsan','Age':28,'Address':'beijing',Tel:'01082658866'}}", 

            success: function(json) { alert(json.d) }, 

            error: function(error) { 

                alert("调用出错" + error.responseText); 
            } 
        });

 

  The results as shown below:

Similar simple procedure call parameter type is a character string by Person person object represented by the JS, sent to the client, WebService automatically string person object

Converted to the Person entity object.

Test three: complex return type for WebService

Copy the code code is as follows:

WebService interface function code is as follows: 

        [the WebMethod (the Description = "Test Methods")] 
        public List <the Person> GetPersonalList () 
        { 
            List <the Person> persons = new new List <the Person> 
                                    { 
                                        new new the Person {the Address = "Beijing", Age = 25 , the Name = "zhangshan", Tel = "01,082,678,866"} 
                                    }; 
            return persons; 
        } <BR> JQ calling code as follows: 

            $ .ajax ({ 

            type: "the POST", 

            URL: "WebService1.asmx / GetPersonalList", 

            dataType: "json",   

            contentType: "application/json; charset=utf-8", 

            success: function(json) { $(json.d).each(function() { alert(this.Name + "-" + this.Age + "-" + this.Address + "-" + this.Tel) }) }, 

            error: function(error) { 

                alert("调用出错" + error.responseText); 

            } 

        });

 

  As shown below:

That complex return type, simple type treatment is essentially the same.

I have heard there is a perception that, when Jq call WebSevice, using JSON as the data exchange format, return data type must be serializable. Is it really. ?

The basic data types .Net indeed be serializable, which is no doubt. So List <T> data type if it can be serialized. ? See in List <T> metadata (the Metadata) Information

I will know. .

[DebuggerTypeProxy(typeof (Mscorlib_CollectionDebugView<T>))]

[DebuggerDisplay("Count = {Count}")]

[Serializable]

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

{

/**/

}

If the above statements are true, in this case, the call was successful is understandable. But the problem really the case. ? Continue following test:

Test Four: complex return type for WebService

Copy the code code is as follows:

          [WebMethod(Description = "测试方法")] 
        public Person GetPerson() 
        { 
            Person person = new Person {<BR>                               Address = "beijing", Age = 27, <BR>                               Name = "zhangshan", Tel = "01082678866"                               <BR>                              }; 
            return person; 
        } 

JQ调用代码如下: 

        $.ajax({ 

            type: "POST", 

            url: "WebService1.asmx/GetPerson", 

            dataType: "json", 

            contentType: "application/json; charset=utf-8", 

            //data: "{'person':{'Name':'zhangsan','Age':28,'Address':'beijing',Tel:'01082658866'}}", 

            success: function(json) { $(json.d).each(function() { alert(this.Name + "-" + this.Age + "-" + this.Address + "-" + this.Tel) }) }, 

            error: function(error) { 

                alert("调用出错" + error.responseText); 

            } 

        });

 

  As shown below:

However, in the test four, GetPerson () method returns the data type Person. Look at the definition of Person entity, it is not marked ask serializable.

From the results: JQ call WebService, you do not necessarily need to return a complex data type must be serializable.

What follows is an interesting test. We all know WebService return type can not be Hashtable type. Because it implements the interface because it implements IDictionary.

Test five: the return type for complex WebService

Copy the code code is as follows:

         [WebMethod(Description = "测试方法")] 
        public Hashtable GetPersonalHashtable() 
        { 
            Hashtable hashtable = new Hashtable(); 

            Person person = new Person { Address = "beijing", Age = 25, Name = "zhangshan", Tel = "01082678866" }; 

            hashtable.Add(1, person); 

            return hashtable; 
        } 

JQ调用代码如下: 

        $.ajax({ 

            type: "POST", 

            url: "WebService1.asmx/GetPersonalHashtable", 

            dataType: "json", 

            contentType: "application/json; charset=utf-8", 

            data: data, 

            success: function(json) { $(json.d).each(function() { alert(this["one"].Name) }) }, 

            error: function(error) { 

                alert("调用出错" + error.responseText); 

            } 

        });

 

 

 

In this way, Jq actually be able to call succeeds. This is somewhat unexpected.

to sum up:

1, a WebService Jq time between the data exchange in the form as JSON, contentType: "application / json; charset = utf-8" must be specified.

WebService do not know what else to as data conversion.

2, Jq WebService return call complex data types do not necessarily need to be a sequence of the type.

3, JSON WebService return data acquisition test as described above alert (json.d) by ".d"

Guess you like

Origin www.cnblogs.com/wfy680/p/12174423.html