繁体   English   中英

jQuery AJAX JSON对象不起作用

[英]jQuery AJAX JSON object not working

我的AJAX请求遇到了一些麻烦。 问题在于名为html的JSON对象。

AJAX请求:

$.ajax({ 
    url      : 'index',
    type     : 'POST',
    dataType : 'json', // Makes no difference
    data     : {
        method   : 'saveModule',
        html     : content
    }, 
    success : function(i){
        console.log(i);
    } 
})

我知道它与html JSON对象有关,因为如果删除它,请求将成功。

这就是萤火虫的console.log();

该对象存储在[]中,正常吗?

[Object { name="Home", link="/home"}, Object { name="Work", link="/work", childs=[3]}, Object { name="Contact", link="/contact", childs=[2]}]

子对象也是JSON对象。

请帮助,这使我发疯!

Web控制台出现的错误:

[11:58:47.215] uncaught exception: [Exception... "Could not convert JavaScript argument"  nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"  location: "JS frame :: http://localhost/mcms/htdocs/templates/resources/js/jquery-1.6.3.min.js :: <TOP_LEVEL> :: line 5"  data: no]

内容var就是这样子的:

 var content =  mcms.module.analyse(obj); // obj is a dom element, in this case a UL with sub ULs inside LIs

函数本身:

 analyse : function (that) {
        return $(that).children('li').map(function() {
            var b = {
                name: $(this).children('a').text(), 
                link: $(this).children('a').attr('href')
            };

            if ($(this).children('ul').size() > 0) {
                b.childs =  mcms.module.analyse($(this).children('ul'));
            } 
            return b;
        });
    }

我已找到问题并解决!

问题是.map()函数返回JSON对象周围的数组。 因此,我在地图周围制作了一个带有计数器的JSON对象以捕获并返回它:)

感谢大家的帮助!

analyse : function (that) {
        var b = {};
        var x = 0;
        $(that).children('li').map(function() {
            b[x] = {
                name: $(this).children('a').text(), 
                link: $(this).children('a').attr('href')
            };

            if ($(this).children('ul').size() > 0) {
                b[x].childs =  mcms.module.analyse($(this).children('ul'));
            } 
            x++;
        });
        return b;
    }

我不太确定method参数。 如果这是您要调用的方法,则也可以在URL中包括它,对吗?

好吧,您当前对$.ajax调用看起来不太正确。 应该是以下内容:

$.ajax({ 
    url      : 'index',
    type     : 'POST',
    data     : <data to be sent>
    dataType : <Default: Intelligent Guess (xml, json, script, or html)>
    success : function(i){
        console.log(i);
    } 
})

jQuery网站上的更多信息: http : //api.jquery.com/jQuery.ajax/

编辑

好的,我看到您更正了电话。 现在看起来好多了。 在将content转换为JSON对象之前, content从何而来?

编辑2

好吧,我认为这个答案应该对您有所帮助: 使用JSON将嵌套对象发布到Spring MVC控制器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM