.net 4.5 ASP.Net web API JSONP support

10,670

Solution 1

As far as I know you must get the jsonp formatter. Here is one of the implementations:

http://nuget.org/packages/WebApi.JsonP

UPDATE

The recommended package is provided now by WebApiContrib team:

https://www.nuget.org/packages/WebApiContrib.Formatting.Jsonp

Add it to Global.asax on application start:

GlobalConfiguration.Configuration.Formatters.Insert(0,  new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter()));

Example of usage with jquery can be found in here

Solution 2

This was the first post I found and was the most helpful but it still left a a few questions and I found a few problems with some of the answers, while I was able to make it work because of all the answers, I wanted to add what I learned because it seem to be high in the search returns.
I installed https://www.nuget.org/packages/WebApiContrib.Formatting.Jsonp and was able to get it to work after I added

GlobalConfiguration.Configuration.Formatters.Insert(0,  new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter()));

to my Global.asax on application startas Alex Wheat and mbudnik answered, it caused the CORS to stop working on the other parts of my API that have all ready been implemented.

I now have it all working properly and figured out a few things from the package developer's github.

after installing the nuget package, Add the following to your Global.asax file

GlobalConfiguration.Configuration.AddJsonpFormatter();

then if you call the service using the below Jquery, just replace the url with your url and you can see the results in you console. I also recommend getting Fiddler Web Debugger installed because it will help you troubleshoot.

$.ajax({
    url: 'http://jsfiddle.net/echo/jsonp/',
   type: 'GET',
    contentType: 'text/javascript',
    crossDomain: true,
    success: function(data) {
        console.log(data);
    },
    error: function(jqXHR,status,error) {
    console.log(error);
    }
});

The contentType: 'text/javascript' is important. This is what tells the web api to use the Jsonp Formatter.

Do not include a dataType of 'jsonp' - Example below

     ...type: 'GET',
    dataType: 'jsonp',
    crossDomain: true,... 

this will cause the web api to use the JsonMediaTypeFormatter and you will get the "jQueryrandomfunctionstring was not called parsererror" error. I know this from personal trial and error. Hopefully this helps someone else.

Solution 3

I like this WebApiContrib.Formatting.Jsonp NuGet package

Add this formatter into Application_Start:

GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter()));
Share:
10,670

Related videos on Youtube

user2526824
Author by

user2526824

Updated on September 14, 2022

Comments

  • user2526824
    user2526824 over 1 year

    Does anyone know if returning JSONP is supported out of the box in .net 4.5 ASP.NET WEB API?

    I've found plenty of "roll your own" for earlier versions of MVC or .net but there doesn't seem to be anything specific to later versions.

    I realize this could be because they earlier versions will work with .net 4.5 stack but I'm curious if someone has already been baked in.

  • sleath
    sleath over 9 years
    Thanks I was having the same issue this resolved it. I see around the internet jsonp is a little light on documentation compared to JSON. Are there any links besides the one found in your post that you've found good for implementation?
  • mbudnik
    mbudnik over 9 years
    JSONP is actually a JSON object but it is wrapped in a Javascript function call specified by the client (the callback parameter). I don't think any documentation for it is neccessary. It just works using the formatter and any jsonp aware javascript library (like jQuery). I found a Stackoverflow post of someone explaining it in more detail: stackoverflow.com/questions/2067472/what-is-jsonp-all-about. Also updated my answer with the new recommended package as of now.