2010-04-13 77 views
0

我的JS是:jQuery的Ajax调用返回错误时,它不是一个错误

$(InitFavorite); 

function InitFavorite(){ 

    var jList = $(".favourite_link"); 
    var ids_to_check = {};//new Array(); 

    $.each(jList, function() { 
     var id = this.id; 
     var object = id.split("_"); 
     if (!ids_to_check[object[1]]) { 
      ids_to_check[object[1]] = []; 
     } 
     ids_to_check[object[1]].push(object[0]); 
    }); 

    //console.log(ids_to_check); 

    $.ajax({ 
     type: 'POST', 
     url: '/user/subscription/favourite-listing', 
     data: ids_to_check, 
     dataType: 'json', 
     beforeSend: function(x) { 
       if(x && x.overrideMimeType) { 
       x.overrideMimeType("application/j-son;charset=UTF-8"); 
      } 
     }, 
     error: function() { 
      alert(1); 
     }, 
     success: function() { 
      alert(2); 
      /*$each(returned_values, function() { 
       alert('boom'); 
      });*/ 
     } 
    }); 
} 

从Ajax调用,将返回以下数据:

{"env":"development","loggedIn":true,"translate":{}}{"Playlist":{"10":"Stop Recieving Updates For This Playlist"},"Clip":{"26":"Recieve Updates For This Clip","27":"Recieve Updates For This Clip","28":"Recieve Updates For This Clip","29":"Stop Recieving Updates For This Clip","30":"Recieve Updates For This Clip"}} 

然而,成功永远不会触发,只是错误,尽管没有标题和json作为标题放出(通过zend框架)。

想法?

回答

4

引用的JSON无效,只能有一个顶级对象,它必须包含其他所有内容(如属性)。有关the JSON site的详细信息。

{"env":"development","loggedIn":true,"translate":{}}{"Playlist"... 
                ^-- here's the error 

查看错误函数errorThrown parameter总是一个好主意。在这种情况下,它会标记来自JSON解析器的错误(Chrome中的“Unexpected token:{”,IE中的“Expected';'”,FF中的“SyntaxError:JSON.parse”等)。

+0

谢谢! .... – azz0r 2010-04-13 17:27:59