jquery's. Post Solutions

Source: Internet
Author: User

jquery's. Post Solutions (i)

Preparatory 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;}
}

Jquery.post (URL, [data], [callback], [type])

URL: The address of the load page

data (optional): k/v pairs or serialized string (. Serialize ()), parameter

CALLBAKC (optional): Execution function after data successfully loaded

type (optional): Request returned data format, String type

(i) ashx file

(1) Request single Entity Data

· ASHX file, which does not explicitly serialize the returned data.

Customer customer = new Customer
{Unid = 1, CustomerName = "Song Jiang", Memo = "Day Kui xing", other = "Black Three Lang"};

Context. Response.Write (Customer);

Ajax Post

function Getcustomer_ashx () {
$.post (
"Webdata/post_1.ashx",
function (data) {
var SX = $. Jsontoobject (data);
var tt = "";
$.each (SX, function (k, v) {
tt + = k + ":" + V + "<br/>";
})
$ ("#divmessage"). HTML (TT);
},
"JSON"
);}

(2) Request entity Collection

ashx File

Customer customer = new Customer
{Unid = 1, CustomerName = "Song Jiang", Memo = "Day Kui xing", other = "Black Three Lang"};

Customer Customer2 = new Customer
{Unid = 2, CustomerName = "WU", Memo = "Secret Star", other = "mastermind"};


list<customer> _list = new list<customer> ();
_list. ADD (customer);
_list. ADD (CUSTOMER2);
String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (_list);

Context. Response.Write (Strjson);

Ajax Post

function Getcustomer_ashxlist () {
$.post (
"Webdata/post_1.ashx",
function (data) {
var jsonobjects = $.jsontoobject (data);
var tt = "";
$.each (jsonobjects, function (k, v) {
$.each (V, function (KK, VV) {
tt + = KK + ":" + vv + "<br/>";
});
});

$ ("#divmessage"). HTML (TT);
},
"JSON"
);
}

(3) Request with parameters

ashx File

A FETCH statement for the request parameter is added on the former basis, and a LINQ query is added

int Icustomerid = Convert.ToInt32 (context. request["Iunid"]);
var cus = from Q in _list
where Q.unid = = Icustomerid
Select Q;

String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (cus);

Ajax Post

function Getcustomer_ashxwithpara () {
$.post (
"Webdata/post_1.ashx",
{iunid:1},
function (data) {
var tt = "";
$.each (data, function (k, v) {
$.each (V, function (KK, VV) {
tt + = KK + ":" + vv + "<br/>";
});
});
$ ("#divmessage"). HTML (TT);
},
"JSON"
);}

Note that the directly returned JSON object [Object,object] can be parsed directly.

The method of passing this parameter is to pass the format in k/v, and the post has another way, that is, the string after the. Serialize ().

(ii) Web Service

(1) Hello

•ws

[WebMethod]
public string HelloWorld ()
{
Return "Hello World";
}

Ajax Post

function Webservice_hello () {
$.post (
"Post_1.asmx/helloworld",
function (data) {
alert (Data.text);
$ ("#divmessage"). HTML (data);
},
"JSON"
);}

This Web method returns a separate string. This is a pure string, for the client, this is an object, but it can also be understood as a [Object,object] object, and its complete data format can be understood as: {text: "Hello World"}

So the access to it here can be as follows:

Data.text This way corresponds to Object.property

data["Text"] This method corresponds to object["key"]

(2) JSON string

•ws

[WebMethod]
public string Helloworld_json ()
{
String strjson=
@ "{unid:1,customername:" "Song Jiang" ", Memo:" Day Kui xing "", Other: "" Black Three Lang ""} ";
return Strjson;
}

Ajax Post

function webservice_hellojsonstring () {
$.post (
"Post_1.asmx/helloworld_json",
function (data) {
var jsonstring = Data.text;
var jsonobject = $.jsontoobject (jsonstring);
var tt = "";
$.each (Jsonobject, function (k, v) {
tt + = k + ":" + V + "<br/>";
})

$ ("#divmessage"). HTML (TT);
},
"JSON"
);}

Although the service method returns data of type string:

{unid:1,customername: "Song Jiang", Memo: "Tian Kui xing", Other: "Black Three Lang"}

But the data the client gets is the object type, which can be understood as [Object,object], which is

{text: ' {unid:1,customername: ' Song Jiang ', Memo: "Tian Kui xing", Other: "Black Tri Lang"} '}

The data requested by the client is taken to the JSON string, which is then converted to a JSON object and parsed.

Therefore, when requesting a Web service method, if the method returns a string type, it is necessary to get the V value of the unique k/v pair via Data.text, which is the JSON string, and then proceed to the next step.

(3) A Web method that returns a JSON string by serialization

•ws

[WebMethod]
public string Getcustomer_json ()
{
Customer customer = new Customer
{Unid = 1, CustomerName = "Song Jiang", Memo = "Day Kui xing", other = "Black Three Lang"};

String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (customer);
return Strjson;
}

Ajax Post

function webservice_customerjsonstring () {
$.post (
"Post_1.asmx/getcustomer_json",
function (data) {
var jsonstring = Data.text;
var jsonobject = $.jsontoobject (jsonstring);
var tt = "";
$.each (Jsonobject, function (k, v) {
tt + = k + ":" + V + "<br/>";
})
$ ("#divmessage"). HTML (TT);
},
"JSON"
);}

This method is the same as (2).

(4) Customer set

•ws

[WebMethod]
public string Getcustomerlist_json ()
{
Customer customer = new Customer
{Unid = 1, CustomerName = "Song Jiang", Memo = "Day Kui xing", other = "Black Three Lang"};

Customer Customer2 = new Customer
{Unid = 2, CustomerName = "WU", Memo = "Secret Star", other = "mastermind"};

list<customer> _list = new list<customer> ();
_list. ADD (customer);
_list. ADD (CUSTOMER2);

String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (_list);
return Strjson;
}

Ajax Post

function webservice_customerlistjsonstring () {
$.post (
"Post_1.asmx/getcustomerlist_json",
function (data) {
var jsonstring = Data.text;
var jsonobject = $.jsontoobject (jsonstring);
var tt = "";
$.each (Jsonobject, function (k, v) {
$.each (V, function (KK, VV) {
tt + = KK + ":" + vv + "<br/>";
});
});
$ ("#divmessage"). HTML (TT);
},
"JSON"
);}

Actually get the JSON string, also can parse out normally. The main idea is to understand the format of the returned data object.

(5) WS with parameters

•ws

[WebMethod]
public string Getcustomerlist_jsonpara (int iunid)
{
Customer customer = new Customer
{Unid = 1, CustomerName = "Song Jiang", Memo = "Day Kui xing", other = "Black Three Lang"};

Customer Customer2 = new Customer
{Unid = 2, CustomerName = "WU", Memo = "Secret Star", other = "mastermind"};

list<customer> _list = new list<customer> ();
_list. ADD (customer);
_list. ADD (CUSTOMER2);

var cus = from Q in _list
where Q.unid = = Iunid
Select Q;

String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (cus);
return Strjson;
}

Ajax Post

function Webservice_customerlistjsonstringwithpara () {
$.post ("Post_1.asmx/getcustomerlist_jsonpara",
{Iunid:2},
function (data) {
var jsonstring = Data.text;
var jsonobject = $.jsontoobject (jsonstring);
var tt = "";
$.each (Jsonobject, function (k, v) {
$.each (V, function (KK, VV) {
tt + = KK + ":" + vv + "<br/>";
});
});
$ ("#divmessage"). HTML (TT);
}
);}

With the parameter post, the type part of the Post function cannot be returned in a JSON format request. can be omitted.

jquery's. Post Solutions

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.