JSONP response from WebAPI to Angular

I have a simple angular controller like below:

angular.module('widget.controllers', [])
  .controller('ProductController', ['$scope','$http', function ($scope, $http) {

      $http.jsonp('http://localhost:27467/api/Products/GetProducts/EUR?callback=JSON_CALLBACK')
      .success(function (data) {
          console.log(data);
      });

  }]);

along with a simple action in web api controller:

[HttpGet]
public HttpResponseMessage GetProducts(string id)                
{
      var returnData = data;
      string callback = ControllerContext.Request.GetQueryNameValuePairs().Where(kv => kv.Key == "callback").Single().Value;    
      return Request.CreateResponse(HttpStatusCode.OK, returnData, new JsonpMediaTypeFormatter(new JsonMediaTypeFormatter(),callback), new MediaTypeHeaderValue("text/javascript"));
}

where I am using this formatter. But the response is not containing the callback function's name wrapped around the JSON. It produces sth like:

({.....});

instead of:

myCallback({....});

Please suggest for a solution.

Answers


Used this to have control over which apis to allow CORS or not. Epic!


Need Your Help

What's the difference between “Any iOS SDK” and “Debug” in Xcode 4 Code Signing?

iphone xcode build xcode4 provisioning

In Xcode 4's Build Settings, there are 2 places to enter a provisioning profile for each build type. The first is "Ad Hoc", "Debug" and "Release". Each of these has another place to put in a provis...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.