2011-02-03 13 views
0

我正在嘗試新的jQuery 1.5,它在我的應用程序中破壞了一些東西。我打電話給一個產生JSON的動作,但是有些出錯並導致腳本停止。根據Fiddler和Firebug,該操作確實會返回JSON數據。我沒有提供JSON數據,但根據JSONLint,數據是有效的。

請注意,這符合jQuery 1.4.4的預期。

我注意到的第一件事情就是網址:http://localhost:3219/News/GetAllNewsArchives?callback=jQuery15033185029088076134_1296751219270&_=1296751219672

腳本:

// Dropdown box for past articles 
$("#article-select").ready(function() { 
    $.ajaxSetup({ cache: false }); 
    $.getJSON('/News/GetAllNewsArchives', null, function(json) { 
     var items = "<option value=''>(Select)</option>"; 
     $.each(json, function(i, item) { 
      items += "<option value='" + item.Id + "'>" + subject + "</option>"; 
     }); 
     $("#article-select").html(items); 
    }); 
}); 

操作:

public ActionResult GetAllNewsArchives() 
    { 
     return Json(newsRepository.GetAllNewsArchives(), JsonRequestBehavior.AllowGet); 
    } 

什麼我做錯了任何想法?

回答

3

好吧,我切換到$就調用和我有同樣的錯誤:

// Dropdown box for past articles 
$("#article-select").ready(function() { 
    $.ajax({ 
     cache: false, 
     type: "POST", 
     dataType: "json", 
     url: "/News/GetAllNewsArchives", 
     success: function(json) { 
      var items = "<option value=''>(Select)</option>"; 
      $.each(json, function(i, item) { 
       items += "<option value='" + item.Id + "'>" + subject + "</option>"; 
      }); 
      $("#article-select").html(items); 
     } 
    }); 

不過,我注意到在$.ajax() documentation東西。

As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

我改變了我的數據類型從dataType: "json"dataType: "text json"然後它的工作。

現在,我只是不明白爲什麼會有所不同。 json期望有什麼不同?

+2

找到[jQuery.com論壇](https://forum.jquery.com/topic/getjson-and-jquery-1-5)中報告的已確認錯誤。 [見補丁](https://github.com/jzaefferer/jquery-validation/issues#issue/36)。 – 2011-02-07 16:03:09

0

出於某種原因,它將您的請求解釋爲JSONP。在Firebug中,檢查$ .ajaxSettings的值並確保沒有將其數據類型默認爲jsonp

您是否嘗試過直接使用$.ajax()來明確設置請求的type,dataType等?

+0

這是我的下一個步驟。但我們不確定我們是否得不到相同的結果。我會嘗試併發布結果。 – 2011-02-03 17:46:56

0

響應中的內容類型是什麼?不是你要求的,或者在dataType parm上指定的,但服務器作爲響應的內容類型(來自fiddler)發回的內容是什麼

+0

`Content-Type:application/json; charset = utf-8` – 2011-02-04 20:01:20

2

在jquery 1.5源代碼中,這是檢測內容的代碼類型。

// Remove auto dataType and get content-type in the process 
while(dataTypes[ 0 ] === "*") { 
    dataTypes.shift(); 
    if (ct === undefined) { 
     ct = jXHR.getResponseHeader("content-type"); 
    } 
} 

// Check if we're dealing with a known content-type 
if (ct) { 
    for (type in contents) { 
     if (contents[ type ] && contents[ type ].test(ct)) { 
      dataTypes.unshift(type); 
      break; 
     } 
    } 

如果將dataType設置爲「json」,則前面的代碼會將ct保留爲未定義狀態。 這就是爲什麼它不能按預期工作。 這是jQuery 1.5中的一個問題,因爲它打破了以前版本jQuery的兼容性。

因此,您應該將dataType設置爲「text json」或刪除dataType,以便使用默認值。

默認值:

converters: { 

     // Convert anything to text 
     "* text": window.String, 

     // Text to html (true = no transformation) 
     "text html": true, 

     // Evaluate text as a json expression 
     "text json": jQuery.parseJSON, 

     // Parse text as xml 
     "text xml": jQuery.parseXML 
    } 
+0

如何在$ .getJSON方法上做到這一點?或者我如何發送正確的內容類型? – 2011-02-07 14:58:21