2013-09-25 44 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