Ajax example: Text in ASP. NET and JSON Response Processing

Source: Internet
Author: User

In the Ajax response data processing part of the text format, the content of the entire text is returned to the Web Client for processing, while the JSON format processing in the text format is slightly more complex than the general text.

Text Format

Let's take a look at an example of text format: first, bind each coupon data, traverse it through the list control, and then send the ID as a get querystring to the server for processing, the format of the data returned by the server is text processing.

To solve the problem, we need to use the shopping cart function below. After clicking the "add to shopping cart" button, users do not need to refresh the entire page and feel a delay of several seconds. First, let's see how ID data values are placed when you click "add to shopping cart. ASP. NET code:

<Button onclick = 'addshopcart ("getshopcart. ashx? Id = <% # eval ("ID") %> ") '> Add to shopping cart </button> <Div id =" Results "> </div>

The following is the core code of the Javascript request object [1:

document.getElementById('results').innerHTML = request.responseText;

In this line, add the text content that the server responds to the client request to the DIV container whose ID is results. The server code is as follows:

int id = int.Parse(context.request.QueryString["id"].ToString());DataTable shopcartTable = DBAccess.GetShopcart(id);context.Response.Write("<span>"+shopcartTable.Rows[0]["name"].ToString()+"</span>");

Here, dbaccess. getshopcart (ID) returns a record. In the text file, you can write the HTML format and return it to the response text (responsetext ).

JSON format

Next, we will discuss the processing of JSON format.

1. What is the syntax of JSON format? See the following JS script code:

{'n': '1'}

Note: In addition to the above JSON format, there is also the format of the array (this article is omitted, see: http://www.json.org/json-zh.html for details ).

2. How to Deal with JSON format in ASP. NET?

The JSON format is a text file. After the ashx file (generally processing files) is processed, response. Write is sent to the rendered page, and then the JS script of the client performs asynchronous callback. The following is the sample code of the processrequest method in ashx:

context.Response.ContentType = "text/plain";context.Response.Write("{\'n\':\'1\'}");

Then, the client JS asynchronous callback. The following is the sample code using the mootools framework:

var ajax=new Request.JSON({    method: 'get',    url: 'ajaxjson.ashx',    onComplete: function(obj, jsontext) {        json = JSON.decode(jsontext);        $('result').innerHTML = json.n;    }}).send();

The JSON. Decode () method converts text into JS objects. JSON. n -- use the standard vertex notation to access the name-Value Pair ('1' here) attribute in JSON format. It indicates the following JSON object instance:

var json = { 'n': '1'}

Finally, after JS event-driven, put the above value 1 in the element container with the ID as result.

(End)

Ajax blog articles:

Ajax: getting started with xhr

Ajax instance: XML Processing on ASP. NET Server

Ajax: use Asynchronous callback to refactor ASP. NET development instances

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.