jQuery AJAX JSON对象不工作

jQuery AJAX JSON object not working

本文关键字:工作 对象 JSON AJAX jQuery      更新时间:2023-09-26

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

AJAX请求:

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

我知道这是关于html JSON对象,因为如果我删除它的请求将成功。

这是firebug的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 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;
    }

我不太确定方法参数。如果这是你想调用的方法,你也可以把它包含在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对象之前?

EDIT2

嗯,我认为这个答案应该对你有帮助:Post嵌套对象到Spring MVC控制器使用JSON