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?
We can solve CORS(Cross-origin resource sharing)issue using two ways,
1)Using Jsonp 2)Enabling the Cors
1)Using Jsonp- to use the Jsonp we need to install WebApiContrib.Formatting.Jsonp nuget package and need to add JsonpFormmater in WebApiConfig.cs refer screenshots,
Jquery code
2)Enabling the Cors -
to enable the cors we need to add Microsoft.AspNet.WebApi.Cors nuget package and need to enable cors in WebApiConfig.cs refer screenshot
For more reference, you can refer my sample repo on GitHub using the following link. https://github.com/mahesh353/Ninject.WebAPi/tree/develop
After asking this question, I finally found what I needed, so I am answering it.
I ran across this JsonpMediaTypeFormatter. Add it into the
Application_Start
of your global.asax by doing this:and you are good to go with an JQuery AJAX call that looks like this:
It seems to work very well.
Updated