2016-08-16 59 views
0

我快到導致控制檯解析使用渲染和JBuilder

a= "<%=j render 'api/restaurants/index.json.jbuilder',restaurants: @restaurants %>"; 
json = $.parseJSON("<%= render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>"); 
console.log(json); 


a= "<%=j render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>"; 
json = $.parseJSON("<%= render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>"); 
console.log("console test"); 


a= "<%=j render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>"; 
json = $.parseJSON("<%= render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>"); 
console.log(a); 

的代碼片段以上不呈現在控制檯什麼後不顯示輸出這個奇怪的問題時,沒有控制檯輸出,但下面的代碼片段確實給出了一個控制檯輸出。

a= "<%=j render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>"; 
console.log(a); 


a= "<%=j render 'api/restaurants/index.json.jbuilder', restaurants: @restaurants %>"; 
console.log("console test"); 

看起來像解析停止控制檯輸出任何東西。任何人有任何想法,爲什麼發生這種情況,以及如何解決它

+0

'「<%= rende'和其他的東西不要像JSON給我,我得到 '意外標記<在JSON在位置0' 的錯誤 –

回答

0

jQuery.parseJSON報道:

在一個畸形的JSON字符串導致JavaScript異常傳遞被拋出。

所以,你的api不會返回一個正確的json。

例:

$(function() { 
 
    try { 
 
    var a = $.parseJSON("{test: 1}"); 
 
    console.log(a); 
 
    } catch(err) { 
 
    console.log('Err: ' + err); 
 
    } 
 
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

+0

謝謝你幫我找到了問題,該對象實際上並不需要解析 – Plasmeious