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