JS-json-1

Source: Internet
Author: User

JS-json-1

To process the smarty template as ajax, You Need To splice an html file to append the node.
Previously, the smarty array needs to be processed as json data and returned. The changes on the server side are relatively small:

header(Content-type : application/json)echo json_encode($arr);

The front-end processing is also very simple

$.ajax({    url : http://example.com,    type : GET,    dataType : json,    data : {        //some data    },    success : function(data){        //blog theme    }});

The above is all nonsense. I have said so much, but it is not guaranteed to be correct. I have no documents at hand, so I wrote a blind look. The following is the topic:

Error 1:

Sometimes the format returned by json is as follows:

{210 : {a,b,c},220 : {a : 110 , b : lolo}}

If I use

for(var i = 0; i < data.length; i++ )

In this way, an error is reported because data does not have the length attribute. You should use the following methods to traverse data:

for(var obj in data)

: It indicates whether the property of the object exists.

We need to explicitly put forward that obj represents the key, so you actually need:

data[obj]

But this is obviously an array access method.

Error 2:

var img = data.img;var imgLen = data.img.length;

If the img attribute is not saved, the undefined object is returned. Here, the undefined object does not have the length attribute. Use it when I block it

if(!data.img.length)

This is actually not possible, because it is a mistake.
A json-handle plug-in is recommended.

 

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.