2

I have a simple ApiController I'm testing:

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;
        }
    }
}

And on the client side i'm calling this from a different domain

$.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);
});

My problem is that the JSONP request is allways returning me an error

parsererror Error: jQuery20301899278084596997_1380125445432 was not called

I've been searching arround but still haven't found a solution for this.

EDIT: I forgot to mention that using fiddler the returned values seem correct header is 200 and the content is a JSON array.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
FabioG
  • 2,936
  • 3
  • 31
  • 50
  • jsonp should be treated at the server side . where in the server side you do something like this : `return "myCallBack("+data+");"` ? – Royi Namir Sep 25 '13 at 16:28
  • @RoyiNamir is there anyway that i can know what's the automatic callback value created in the server side? – FabioG Sep 25 '13 at 16:52
  • @RoyiNamir wasn't able to use it but i found a solution. – FabioG Sep 27 '13 at 10:14

3 Answers3

1

Look at WebApiContrib.Formatting.Jsonp to handle correctly your JSONP call.

On package manager console:

Install-Package WebApiContrib.Formatting.Jsonp
Willian Andrade
  • 332
  • 3
  • 11
1

Solved it by creating an ActionFilterAttribute. I found the solution in the answer from 010227leo on this thread JSONP with ASP.NET Web API

Community
  • 1
  • 1
FabioG
  • 2,936
  • 3
  • 31
  • 50
0

To be able to use different output formats in web api, you have to register appropriate Formatters in FormatterConfig . Also add contentType: "application/json" to your ajax call. More in detail explanation can be found here

Max Tkachenko
  • 504
  • 1
  • 6
  • 17