JavaScript爆炸问题

JavaScript爆炸问题

本文介绍了JavaScript爆炸问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行ajax查询并返回JSON,结果之一是名为image.comment的字段.

I am running an ajax query and returning JSON and one of the results is a field named image.comment e.g.

test~63~Dave Sanders~http://graph.facebook.com/998433599/picture?type=large~Dave Sanders~9 minutes ago

我正在使用~作为分隔符

image.comment中可能有多个由----分隔的相似数据集,以便解析数据并将其添加到我正在使用的DOM中:

There maybe be multiple similar sets of data separated by ---- in image.comment so to parse the data and add it to the DOM i'm using:

if(image.comment){

        html += '<div class="msgs_holder'+image.list_id+'">';

        // split comment at ----
        var comString = image.comment;
        var comArray = comString.split("----");

        var lengthArray = comArray.length,
        element = null;

        for (var i = 0; i < lengthArray; i++) {
            element = comArray[i];

            // split indiv comment at ~
            var comUserString = element;
            var comUserArray = comUserString.split("~");

            html += '<div class="msgs_row"><div class="msgs_pic"><a href="/'+comUserArray[4]+'"><img src="'+comUserArray[3]+'"></a></div>';
            html += '<div class="msgs_comment"><a href="/'+comUserArray[4]+'" style="text-decoration:none;"><span class="msgs_name">'+comUserArray[2]+'</span></a> '+comUserArray[0]+'<br><span class="msgs_time" title="'+comUserArray[5]+'">'+comUserArray[5]+'</span></div></div>';

        }

        html += '</div>';

    }

但是页面无法加载,firefox firebug告诉我:allocation size overflow

But the page fails to load and firefox firebug tells me: allocation size overflow

这仅发生在comment列中有数据的JSON行上

This only happens on rows of the JSON where there is data in comment column

我怀疑可能存在无限循环,但看不到

I suspect some maybe infinite looping but can't see it

我已经检查了查询,它运行正常,所以不是

I've checked the query and it runs fine so not that

这是在js中爆炸的最佳方法吗?

Is this the best way to explode in js?

有什么想法吗?

更多信息我这样做的原因是这是一个更大的查询.图像已加载到页面上,每个图像下方都有注释.因此,我将注释加载到一列中进行解析以进行显示.

MORE INFO The reason I'm doing it this way is it's a piece of a much bigger query. Images loaded to a page with comments under each image. So I'm loading the comments into a column to be parsed for display.

示例:

Image 1        Image 2        Image 3                       etc etc etc
hey                           i think its great!
hey back!                     me too
                              I'm not sure

更多信息2 此数据来自MySQL查询,例如

MORE INFO 2 This data is taken from a MySQL query e.g

select w.img_width, w.img_height, w.prod_pic_url as preview3,
        GROUP_CONCAT(ww.comment,'~', h.user_id,'~', h.name,'~',
        h.live_prof_pic,'~', h.twovo_url,'~', time_ago(ww.dateadded) SEPARATOR '----') AS comment
        from tableName where blah=blah

GROUP_CONCAT是上面提到的评论部分

GROUP_CONCAT is the comments section mentioned above

那么,要将JSON用于数据中的数据,有可能吗?如果可以,怎么办?

So, to use JSON for data within data, is that possible? If so how?

示例返回的数据:

"preview3":"http:\/\/myurl.com\/wish_images\/production\/3578.jpg","comment":"test~63~Dave Sanders~http:\/\/graph.facebook.com\/Dave Sanders\/picture?type=large~Dave Sanders~39 minutes ago"},{"item_id":"3559","numComms":"0","numLikes":"0"... etc etc

推荐答案

这不适合用作注释,因此,我将尝试写出我认为人们在使用JSON而不是您的字符串时所说的话:

This isn't suited for a comment, so I'll try to write what I think people are saying with respect to using JSON instead of your strings:

此:

test~63~Dave Sanders~http://graph.facebook.com/998433599/picture?type=large~Dave Sanders~9 minutes ago

可能会变成这样:

[
    {
        'name': 'test',
        'id': 63,
        'name': 'Dave Sanders',
        'url': 'http://graph.facebook.com/998433599/picture?type=large',
        'when': '9 minutes ago'
        // other properties
    },
    {
        // the next one
    },
    {
        // etc.
    }
]

它看起来可能需要更多工作,但是您可以为php使用一些JSON库(我猜是因为您使用了术语"explode")在服务器上构建JSON.然后,您返回的内容已经采用了一种形式,供您基于浏览器的客户端应用程序使用,而无需付出额外的努力.我很难想象这最终将比您尝试使用字符串操作做更多的工作,而且JSON可能不那么脆弱.

It may look like more work, but you could use some JSON library for php (I'm guessing, since you use the term "explode") to build the JSON on the server. Then your returned content is already in a form for your browser-based client app to use with no additional effort. I find it hard to imagine that this will be more work in the end than what you're trying to do with the string manipulation, and JSON is likely to be less brittle.

这篇关于JavaScript爆炸问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 02:19