Home > Web Front-end > JS Tutorial > jQuery uses the ajax method to parse the returned json data function example

jQuery uses the ajax method to parse the returned json data function example

高洛峰
Release: 2017-01-12 10:15:54
Original
1152 people have browsed it

The example in this article describes the function of jQuery using the ajax method to parse the returned json data. Share it with everyone for your reference, the details are as follows:

Recently, I found a problem when using jQuery's ajax method to transmit and receive json data, that is, the returned data data can sometimes be used directly as json data. Is there any It’s not the right time. After checking some information, the explanation is as follows:

$.ajax({
  url: ajaxurl,
  type: "POST",
  success: function(data){
   //假设返回的json数据里有status及info2个属性
   //有时候可以直接ajaxobj.status或者ajaxobj["status"]去访问
   //但有时候,却要通过eval()或者 $.parsejson();才可以通过ajaxobj.status访问,而且这种情况下,需要是complete而不是success
   ajaxobj=eval("("+data+")");
   //或者$.parsejson()
   //var ajaxobj = $.parsejson(data);
   if(ajaxobj.status=="0")
   {
    alert("请登陆.");
   }
   else if(ajaxobj.status=="1")//未绑定微博
   {
    alert(ajaxobj.info);
   }
   return true;
  },
  error:function(ajaxobj)
  {
     if(ajaxobj.responseText!='')
     alert(ajaxobj.responseText);
  }
});
Copy after login

Let me explain the first situation first:

If the data. attribute name can be accessed directly, the server-side code must be a constant string directly returned .

What is a constant string? A constant string refers to a string composed directly of "". If a String variable is not defined and a string of "" is printed directly to the front desk, you can directly use the data. attribute name. Access, and you can get it by just writing success on the jquery side.

The following is the reason why eval is required and success cannot be entered:

This situation is because when the server prints out, it is a String object. Usually such problems are in my code. It's because the background json is more complicated. I used StringBuffer when organizing, and then when I printed at the end, I printed the toString of the StringBuffer object, so it was equivalent to printing a String object.

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

Except for this Two points. It should be noted that if you are using jq1.4, then it has stricter requirements for the format of json. All keys and attributes must be marked with double quotes, although the key does not use double quotes natively. js is allowed, but jq1.4 seems to have this requirement.

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

I hope this article will be helpful to everyone in jQuery programming.

For more jQuery using the ajax method to parse the returned json data function examples and related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template