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.

3
  • jsonp should be treated at the server side . where in the server side you do something like this : return "myCallBack("+data+");" ? Commented Sep 25, 2013 at 16:28
  • @RoyiNamir is there anyway that i can know what's the automatic callback value created in the server side? Commented Sep 25, 2013 at 16:52
  • @RoyiNamir wasn't able to use it but i found a solution. Commented Sep 27, 2013 at 10:14

3 Answers 3

1

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

On package manager console:

Install-Package WebApiContrib.Formatting.Jsonp
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.