簡體   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