I am working on creating a new set of services in ASP.MVC MVC 4 using the Web API. So far, it's great. I have created the service and gotten it to work, and now I am trying to consume it using JQuery. I can get back the JSON string using Fiddler, and it seems to be ok, but because the service exists on a separate site, trying to call it with JQuery errors with the "Not Allowed". So, this is clearly a case where I need to use JSONP.
I know that the Web API is new, but I'm hoping someone out there can help me.
How do I make a call to a Web API method using JSONP?
You can use an ActionFilterAttribute like this:
Then put it on your action:
Check this one out. See if it helps.
JSONP with Web API
If the context is
Web Api
, thanking and referring to010227leo
's answer, you must considerWebContext.Current
value which is going to benull
.So I updated his code to this:
JSONP only works with Http GET request. There is a CORS support in asp.net web api which works well with all http verbs.
This article may be helpful to you.
Unfortunately, I don't have enough reputation to comment, so I'll post an answer. @Justin raised the issue of running the WebApiContrib.Formatting.Jsonp formatter alongside the standard JsonFormatter. That issue is resolved in the latest release (actually released some time ago). Also, it should work with the latest Web API release.
Instead of hosting your own JSONP formatter version you can install WebApiContrib.Formatting.Jsonp NuGet package with already implemented one (choose the version that works for your .NET Framework).
Add this formatter into
Application_Start
: