Example of jQuery Parsing Returned json Data Using ajax Method

  • 2021-07-10 18:14:43
  • OfStack

This article illustrates how jQuery uses the ajax method to parse the returned json data. Share it for your reference, as follows:

Recently, when using ajax method of jQuery to transmit and receive json data, a problem was found, that is, the returned data data can sometimes be directly used as json data, but sometimes it can't. After checking some information, the explanation is as follows:

  url: ajaxurl,
  type: "POST",
  success: function(data){
   // Assuming that the returned json There is in the data status And info2 Attributes 
   // Sometimes it can be directly ajaxobj.status Or ajaxobj["status"] To visit 
   // But sometimes, you have to pass eval() Or  $.parsejson(); Before you can pass ajaxobj.status Access, and in this case, you need to be complete Instead of success
   // Or $.parsejson()
   //var ajaxobj = $.parsejson(data);
    alert(" Please log in .");
   else if(ajaxobj.status=="1")// Unbound Weibo 
   return true;

Explain the first case first:

In case of direct data. Attribute name access, server-side code 1 is a constant string of direct return.

What is a constant string? Constant string refers to a string composed of "" directly. If a string of "" print is directly sent to the foreground without defining String variable, it can be accessed directly by data. Attribute name, and jquery can be obtained by writing success.

The following are the reasons why you want eval and cannot enter success:

This situation is because the server side is an String object when print is outward. Usually, this kind of problem in my code is because the background json is complicated, and I use StringBuffer when organizing, and then print is toString of StringBuffer object when print is finally, so it is equivalent to print having an String object

In this case, the ajax method of jquery will not enter the success method, and can only be received by complete. If you want to parse json data in data, you must perform eval () or $. parsejson () on data. responseText;

In addition to these two points, it should be noted that if you use jq 1.4, it has stricter requirements for the format of json. All key and attributes should be marked with double quotation marks. Although key is allowed without double quotation marks, jq 1.4 seems to have this requirement.

The above is my personal thoughts and understanding. If you have different opinions, please give me advice.

PS: Regarding json operation, here we recommend several practical json online tools for your reference:

Online JSON code verification, verification, beautification and formatting tools:

JSON Online Formatting Tool:

Online XML/JSON interconversion tool:

json code online formatting/beautification/compression/editing/conversion tool:

Online json compression/escape tool:

C Language Style/HTML/CSS/json Code Formatting and Beautification Tool:

For more readers interested in jQuery related content, please check the topics on this site: "Summary of Ajax Usage in jquery", "Summary of jQuery Operating json Data Skills", "Summary of jQuery form Operating Skills", "Summary of jQuery Common Plug-ins and Usage", "Summary of jQuery Extension Skills", "Summary of jQuery Table (table) Operating Skills" and "Summary of jquery Selector Usage"

I hope this article is helpful to everyone's jQuery programming.

Related articles: