Several ways to return jquery requests

Source: Internet
Author: User
Tags button type httpcontext

Page code

<form id= "Form1" runat= "Server" >
<div>
<p> Ajax Request ASHX return JSON Data FAQ </p>
<button type= "button" id= "BTNRQ" > click Request Data </button>
</div>
<div id= "Iddiv" ></div>
</form>

1 Request text data, manually parse the first return JSON character in the success event

Front desk:

<script>
The first returns the JSON character
$ (document). Ready (function () {
$ ("#btnrq"). Click (function () {
$.ajax ({
Type: "Post",
URL: "Result.ashx",
Data: {"name": "Li"},
DataType: "Text",
Success:function (data) {
var json = eval (' (' + data + ') ')//Why add the Eval function itself to the problem. Since JSON starts and ends in the form of "{}", in JS, it is treated as a block of statements, so it must be coerced into an expression.
Alert ("Gender:" + json.sex + ", hobby:" + json.interest);
$ ("#iddiv"). Text (json.sex);
}
});

});
});
</script>

Background:

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Text/plain";
Context. Response.Write ("Hello World");
Request (context);
}
void request (HttpContext context) {
var name = context. request["Name"];
if (name = = "Li") {
String json = "{\" sex\ ": \" man \ ", \" interest\ ": \" basketball \ "}";
Context. Response.Write (JSON);
Context. Response.End ();
}
}

2. Request JSON data, manually resolve JQ in success event the second return JSON object is parsed automatically

Front desk:

<script>
The first returns the JSON character
$ (document). Ready (function () {
$ ("#btnrq"). Click (function () {
$.ajax ({
Type: "Post",
URL: "Result.ashx",
Data: {"name": "Li"},
DataType: "JSON",
Success:function (data) {
var json = eval (' (' + data + ') ')//Why add the Eval function itself to the problem. Since JSON starts and ends in the form of "{}", in JS, it is treated as a block of statements, so it must be coerced into an expression.
Alert ("Gender:" + json.sex + ", hobby:" + json.interest);
$ ("#iddiv"). Text (json.sex);
}
});

});
});
</script>

Background:

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Text/plain";
Context. Response.Write ("Hello World");
Request (context);
}
void request (HttpContext context) {
var name = context. request["Name"];
if (name = = "Li") {
String json = "{\" sex\ ": \" man \ ", \" interest\ ": \" basketball \ "}";
Context. Response.Write (JSON);
Context. Response.End ();
}
}

3. With serialized text data, manually parse the returned JSON object with serialization in the Success event

Front desk:

<script>
The first returns the JSON character
$ (document). Ready (function () {
$ ("#btnrq"). Click (function () {
$.ajax ({
Type: "Post",
URL: "Result.ashx",
Data: {"name": "Li"},

DataType: "Text",
Success:function (data) {
$ ("#iddiv"). Text (data);
var json = eval (' (' + data + ') ')//Why add the Eval function itself to the problem. Since JSON starts and ends in the form of "{}", in JS, it is treated as a block of statements, so it must be coerced into an expression.

Alert ("Gender:" + json.sex + ", hobby:" + json.interest);
$ ("#iddiv"). Text (json.sex);
}

});

});
});
</script>

Background:

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Text/plain";
Context. Response.Write ("Hello World");
Request (context);
}
void request (HttpContext context) {
var name = context. request["Name"];
if (name = = "Li") {
String json = "{\" sex\ ": \" man \ ", \" interest\ ": \" basketball \ "}";
JavaScriptSerializer js = new JavaScriptSerializer (); ASP. NET JSON serialization and deserialization can also use JavaScriptSerializer, under the System.Web.Script.Serializatioin namespace, you need to reference System.Web.Extensions.dll .
Js. Serialize (JSON);
Context. Response.Write (JSON);
Context. Response.End ();
}
}

4. JSON with serialized foreground auto-parse: The fourth JSON object that returns serialization

Front desk:

<script>
The first returns the JSON character
$ (document). Ready (function () {

$ ("#btnrq"). Click (function () {
$.ajax ({
Type: "Post",
URL: "Result.ashx",
Data: {"name": "Li"},

DataType: "Text",
Success:function (data) {
$ ("#iddiv"). Text (data);
Alert ("Gender:" + json.sex + ", hobby:" + json.interest);
}


});

});
});
</script>

Background:

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Text/plain";
Context. Response.Write ("Hello World");
Request (context);
}
void request (HttpContext context) {
var name = context. request["Name"];
if (name = = "Li") {
String json = "{\" sex\ ": \" man \ ", \" interest\ ": \" basketball \ "}";
JavaScriptSerializer js = new JavaScriptSerializer (); ASP. NET JSON serialization and deserialization can also use JavaScriptSerializer, under the System.Web.Script.Serializatioin namespace, you need to reference System.Web.Extensions.dll .
Js. Serialize (JSON);
Context. Response.Write (JSON);
Context. Response.End ();
}
}

Several ways to return jquery requests

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.