【问题标题】:jQuery AJAX JSON object not workingjQuery AJAX JSON 对象不工作
【发布时间】:2012-01-07 20:06:57
【问题描述】:

我的 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 对象的,因为如果我删除它,请求就会成功。

这就是 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]

content 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;
        });
    }

【问题讨论】:

  • content 变量在哪里设置?
  • 当您说“这就是 firebug 的 console.log() 的样子”时,您是在谈论 ajax 成功处理程序中 console.log(i) 语句的输出吗?如果不是,那是什么?
  • 是的,就是这样,但只有重要的部分,内容变量

标签: javascript jquery json


【解决方案1】:

我已经找到问题并解决了!

问题在于 .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;
    }

【讨论】:

    【解决方案2】:

    嗯,您当前对$.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 Nested Object to Spring MVC controller using JSON

    【讨论】:

    • 添加了我制作内容变量的信息
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 2011-11-24
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2014-03-28
      • 2010-12-23
      相关资源
      最近更新 更多