2013-09-25 45 views
2

我有一個簡單ApiController我測試:jQuery的AJAX調用.NET網頁API返回錯誤

namespace SMOnline.Controllers 
{ 
    public class DocumentosController : ApiController 
    { 
     Documentos[] docs = new Documentos[] 
     { 
      new Documentos { ID = 1, Tipo = 1, Designacao = "FC001"}, 
      new Documentos { ID = 2, Tipo = 1, Designacao = "FC002"} 
     }; 

     public IEnumerable<Documentos> GetAll() 
     { 
      return docs; 
     } 
    } 
} 

而且在客戶端上我打的是不同的域調用此

$.ajax({ 
    async: true, 
    type: 'GET', //GET or POST or PUT or DELETE verb 
    url: apiUrl + 'Documentos/', // Location of the service 
    dataType: 'jsonp', //Expected data format from server 
}) 
.done(function (data) { 
    // On success, 'data' contains a list of documents. 
    $.each(data, function (key, item) { 
     alert(item.Designacao); 
    }) 
}) 
.fail(function (jqxhr, textStatus, error) { 
    alert(textStatus + " " + error); 
}); 

我的問題是,JSONP請求返回永諾我一個錯誤

parsererror Error: jQuery20301899278084596997_1380125445432 was not called

我一直在尋找角落找尋,但還沒有找到了解決方案。

編輯: 我忘了提及使用fiddler返回的值看起來正確的標題是200和內容是一個JSON數組。

+0

jsonp應該在服務器端進行處理。在服務器端你可以這樣做:'return「myCallBack(」+ data +「);」'? –

+0

@RoyiNamir有無論如何,我可以知道什麼是在服務器端創建的自動回調值? – FabioG

+0

請求[「callback」] –

回答

0

爲了能夠在網絡API使用不同的輸出格式,你必須在FormatterConfig註冊相應的格式化程序。 同時將contentType: "application/json"添加到您的ajax調用中。 更詳細的解釋可以發現here