2013-02-28 49 views
0

我遇到了一個小問題。我正在嘗試使用jQuery $.ajax調用JSON文件來訪問我在線複選並且我的JSON代碼有效。當我進行調用時,它會在JSON中帶有語法錯誤的解析錯誤。

您可以通過訪問http://michael-nolan.com/

在這裏的錯誤是我的javascript:

$(document).ready(function() 
{ 
    $.ajax(
     { type: "GET", 
      url: 'projects/projects.json', 
      dataType: "json", 
     success: function(results) 
     { 
      console.log("Success!"); 
     }, 
     error: function(XMLHttpRequest, textStatus, errorThrown) 
     { 
      console.log(textStatus); console.log(errorThrown); 
     } 
    }); 
}); 

,這裏是我的JSON

{ 
    "projects": 
    [ 
     { 
      "title":"Adobe Suite", 
      "description":"Some stuff", 
      "imgsrc":"img/adobe_suite_description.png" 
     }, 
     { 
      "title":"Gridlock", 
      "description":"Stuff", 
      "imgsrc":"img/gridlock_description.png" 
     }, 
     { 
      "title":"Open Cart", 
      "description":"more stuff", 
      "imgsrc":"img/opencart_description.png" 
     } 
    ] 
} 
+1

您是否使用過Fiddler(http://www.fiddler2.com/fiddler2/)來確保服務器發送了您期望的內容? – 2013-02-28 05:15:50

+0

你能顯示錯誤嗎?還像tmack說試試它http://jsfiddle.net/,以便您可以複製它,我們可以看到它。 http://jsfiddle.net/vqagE/爲我解析罰款。哇,對不起,完全不同。我將不得不自己檢查一下。 – John 2013-02-28 05:17:01

+0

@John我更新了我的問題,並提供了鏈接到網站的問題正在發生。 – Nolski 2013-02-28 05:23:57

回答

1

錯誤有事情做與兩個段落之間的空白(可能是換行符)在你的第二個數組項

...in the game.</p> 
    <p>Gridlock... 
^ 
| 
the problem 
+0

請參閱我的解答。只需用'\ t'替換選項卡,用'\ n'替換換行符 – 2013-02-28 05:41:04

-1

被記錄錯誤聽起來像它的一個問題你的json。嘗試在您的json中將[ ]切換爲{}

+0

問題是項目應該是一個數組。另外,將其設置爲「{}」會使JSON文件無效。 – Nolski 2013-02-28 05:28:50

0

我已經測試過你的代碼和它爲我工作,我也在你的網站上試過它,並給了304 not modified

因此請嘗試使用getJson() jQuery方法。

$.getJSON('projects/projects.json', function(data) { 

}); 
+0

當我使用'$ .getJSON()'它看起來不像它運行回調。當我看着小提琴手它仍在投擲304 – Nolski 2013-02-28 05:33:17

0

當我測試它時,您的代碼工作正常。根據語法錯誤的不同,我可能會檢查json是如何生成的,以及如果導航到projects/projects.json會正確生成json。如果生成了json,請嘗試將其替換爲靜態json並查看它是否有效。

2

在Chrome中工作正常,IE對JSON(所有東西)特別嚴格。

在第二和第三個描述中注意換行符。我懷疑這是IE的失敗。

+0

這肯定是造成這個問題的原因。謝謝! – Nolski 2013-02-28 05:42:10

0

有標籤和換行符在你的JSON,這將導致解析器失敗。與\t和換行與\n替換每個標籤頁實例例如:

示例JSON文件:

{ 
    textWithTabs : "This is text with \t a tab and \n newline" 
} 

的JavaScript:

var o = JSON.parse(json); 

console.log(o.textWithTabs); //This is text with  a tab and ↵ newline 
0

我建議你試試這個:

{ 
"projects": [ 
    { 
     "title": "Adobe Suite", 
     "description": "<p>With years of experience using the Adobe Suite products I have skills that range from creating vector artwork in Adobe Illustrator, arranging web layouts in Adobe Photoshop, and creating print layouts in Adobe InDesign</p>", 
     "imgsrc": "img/adobe_suite_description.png" 
    }, 
    { 
     "title": "Gridlock", 
     "description": "<p>In my Sophomore year in college I worked with Jayson Fitch as an artist on a isometric 2d shooter called Gridlock. We as a team created all of the art assets that are being used in the game.</p><p> Gridlock is still in development and we hope to release it on the OUYA.Check out it's development blog here. <a href='http://gridlock-game.tumblr.com'>www.Gridlock-Game.Tumblr.com</a></p>", 
     "imgsrc": "img/gridlock_description.png" 
    }, 
    { 
     "title": "Open Cart", 
     "description": "<p>As a freelance project I initiated an overhaul of the Legendary Realms Terrain e-commerce storefront. This entailed a complete visual re-branding as well as creating a backend solution to expand online payment options.</p><p> Feel free to check them out at < a href = 'lrterrain.com' > Legendary Realms Terrain < /a></p > ", 
     "imgsrc": "img/opencart_description.png" 
    } 
    ] 
}