How to handle when JQuery ajax returns JSON
json data is a data storage method for real-time data interaction. The most commonly used method is the combination of ajax and json. Below, I will share with you the processing method of JQuery ajax returning JSON (three methods ), friends in need can refer to
Recently when using JQuery's ajax method, the data returned is required to be json data. During the processing process, I encountered the following problems, that is, using different methods When generating json data, how should it be processed in the $.ajax method? The following will explain it in turn. Since I use asp.net, the pages processed are all done in .net! The other methods should be the same.
1. Some basic knowledge of JSON.
Objects in JSON are identified by "{}". A "{}" represents an object, such as {"AreaId":"123"}, and the value of the object is in the form of a key-value pair ( key: value).
"[]" identifies the array, and the data inside the array are separated by ",", such as ["AreaId": "123", "AreaId": "345"].
In many cases it is an array of objects, that is:
[{“AreaId”:”123”},{“AreaId”:”345”}]
In fact, an array is also an object, and the above format can also be written like this:
{“Area”:[{“AreaId”:”123”},{“AreaId”:”345”}]}
This represents an Area object, which has two sub-data. Each sub-data is also an object, and each sub-object is an AreaId.
The definition format of strings and characters in JSON is similar to that of general C-like language definitions. Double quotes define strings and single quotes define characters.
JSON keys are enclosed in double quotes. For example, the "Area" and "AreaId" above are enclosed in double quotes. When constructing a JSON string in some languages, you can Use an escape character to escape double quotes.
First give the json data to be transmitted: [{"demoData":"This Is The JSON Data"}]
1, use ordinary aspx page to process
I think this method is the easiest to process, look at the code below
$.ajax({ type: "post", url: "Default.aspx", dataType: "json", success: function (data) { $("input#showTime").val(data[0].demoData); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); } });
Here is the code for passing data in the background
Response.Clear(); Response.Write("[{\"demoData\":\"This Is The JSON Data\"}]"); Response.Flush(); Response.End();
This processing method will directly parse the passed data into json data , that is to say, the front-end js code here may directly parse these data into json object data instead of string data, such as data[0].demoData,
This json object data is directly used here
2. Using webservice (asmx) to handle this processing method will not treat the passed data as json object data, but as a string. The following code
$.ajax({ type: "post", url: "JqueryCSMethodForm.asmx/GetDemoData", dataType: "json",/*这句可用可不用,没有影响*/ contentType: "application/json; charset=utf-8", success: function (data) { $("input#showTime").val(eval('(' + data.d + ')')[0].demoData); //这里有两种对数据的转换方式,两处理方式的效果一样//$("input#showTime").val(eval(data.d)[0].demoData); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); } });
The following is the asmx method code [WebMethod]
public static string GetDemoData() { return "[{\"demoData\":\"This Is The JSON Data\"}]"; }
This processing method here treats the json data passed back as a string. This data must be eval processed so that it can become a real json object data.
3, using ashx files to process this method is the same as ordinary aspx page processing, so I won’t explain it here!
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to solve the Chinese garbled code submitted by JQuery ajaxSubmit
##What to do when JQuery ajax returns json with Chinese garbled code Solve
How to use the ajax post method in Django framework
##
The above is the detailed content of How to handle when JQuery ajax returns JSON. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

JSON (JavaScriptObjectNotation) is a lightweight data exchange format commonly used for data exchange between web applications. When processing JSON data, we often encounter Unicode-encoded Chinese characters (such as "u4e2du6587") and need to convert them into readable Chinese characters. In PHP, we can achieve this conversion through some simple methods. Next, we will detail how to convert JSONUnico

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
