Home > Web Front-end > JS Tutorial > Detailed explanation of the get method in jquery

Detailed explanation of the get method in jquery

青灯夜游
Release: 2021-01-02 18:00:27
forward
4143 people have browsed it

Detailed explanation of the get method in jquery

Recommended tutorial: jQuery tutorial

Preparation work

·Customer class

public class Customer
{
    public int Unid { get; set; }
    public string CustomerName { get; set; }
    public string Memo { get; set; }
    public string Other { get; set; }
}
Copy after login

·Server side Processing (Json_1.ashx)

Customer customer = new Customer { Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
context.Response.Write(strJson);
 
jQuery.get( url, [data], [callback], [type] )
Copy after login

Can request data through http get. The callback is an abbreviation of $.ajax, which is called after the data is successfully loaded.

(1) ashx file

Get request to get json data

·Ashx file is no longer provided

·js

function GetCustomer_Ashx() {
    $.get(
    "webdata/get_1.ashx",
    {},
    function(data) {
        var tt = "";    
        $.each(data, function(k, v) {
            tt += k + ":" + v + "<br/>";
        })
        $("#pmessage").html(tt);
    },
    "json"
    );
}
Copy after login

among them , if there are no parameters, then keep the empty parameter list; the data format is set to json

(2) ashx file, the collection

·ashx file is no longer provided, please see my blog Other essays

·js

function GetCustomerList() {
    $.get(
    "webdata/get_1.ashx",
    {},
    function(data) {
        var tt = "";
        $.each(data, function(k, v) {
            $.each(v, function(kk, vv) {
                tt += kk + ":" + vv + "<br/>";
            });
        });
        $("#pmessage").html(tt);
    },
    "json"
    );
}
Copy after login

Among them, there are no parameters, the parameter supply part can be empty, or an empty list can be provided; the data format can be omitted, json can also be written, and there are several other options also.

(3) Request text file

This time the text content is obtained, but the json object is not obtained.

function GetCustomer_txt() {
    $.get(
    "webdata/get_1.txt",
    function(data) {     
        $("#pmessage").html(data);
    },
    "text"
);
}
Copy after login

The data type here can be omitted.

(4) Request WebService

http get method requests web service, get is turned off by default. To be started manually.

Get support can be added in the config file:

<webServices>
      <protocols>
        <add name="HttpGet"/>
      </protocols>
</webServices>
Copy after login

[WebMethod]

public string GetCustomer()
    {
        Customer customer = new Customer
{ Unid = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" };
        string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
        return strJson;
}
Copy after login

The ScriptMethod attribute tag is used to specify the HTTP verb used to call the method and the format of the response. This property is used to specify information about methods that can be called from client script. Use this property to specify the HTTP verb (GET or POST) that can be used to invoke the method. It also lets you specify whether you want to format the response using JavaScript Object Notation (JSON) or XML.

·UseHttpGet

Specifies whether the method is to be called by using the HTTP GET command. The default value is false.

·ResponseFormat

Specifies whether to serialize the response as JSON or XML. The default value is Json. When a method returns an XmlDocument or XmlElement object, the ResponseFormat property can be used to specify XML as the return type.

·XmlSerializeString

Specifies whether all return types (including string types) are serialized to XML. When serializing a response to JSON, the value of the XmlSerializeString property is ignored.

If the web service method does not modify this tag, it will be serialized into a json object by default.

function GetCustomer_Webservice() {
    $.get(
    "get_1.asmx/GetCustomer",
    function(data) {
        var jsonObject = $.jsonToObject(data.text);
        var tt = &#39;&#39;;
        $.each(jsonObject, function(k, v) {
            tt += k + ":" + v + "<br/>";
        });
        $("#pmessage").html(tt);
    },
    "json"
);}
Copy after login

That’s it for this example. When requesting web services through ajax get, be sure to enable get protocol access.

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of Detailed explanation of the get method in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template