0

This question is specific to Asp.Net WebApi.

In my global file, I've put:

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonpMediaTypeFormatter());

JsonpMediaTypeFormatter is custom Jsonp formatter that I have.

After this, when i call,

function jsonpCallback(data) {
        alert('in' + data);
        var list = $('#courses');
        for (var i = 0; i < data.length; i++) {
            var course = data[i];
            list.append('<li>' + course.name + '</li>');
        }
    }
$.getJSON("http://localhost:64009/api/courses?callback=?", null, jsonpCallback);

It worked.

But, from my global file, if I remove

GlobalConfiguration.Configuration.Formatters.Clear();

Then it doesn't work.

What if i want to support multiple formatters? As I can use HttpClient for CORS and ask for xml or json or jsop?

Can someone please advice?

Dante
  • 3,833
  • 4
  • 38
  • 55
SCV
  • 1
  • 2
  • You should get your question and post it here. If the other site is down, then people won't be able to see it. – Dante May 06 '12 at 18:33
  • As per request: I've added jsonp formatter to my GlobalConfiguration GlobalConfiguration.Configuration.Formatters.Add(new JsonpMediaTypeFormatter()); Then i try to call my webapi using format that's not supported (dataType: "x-vcard")..that means it should return json: – SCV May 06 '12 at 18:37
  • $.ajax({ url: "/api/courses", dataType: "x-vcard", type: "GET", success: function (data) { var list = $('#courses'); for (var i = 0; i < data.length; i++) { var course = data[i]; list.append('
  • ' + course.name + '
  • '); } }, error: function (jqXHR, textStatus, errorThrown) { alert('error'); alert(jqXHR.statusText + " ----- " + jqXHR.responseText); } }); – SCV May 06 '12 at 18:41
  • Response from fiddler: 49 [{"id":0,"name":".net"},{"id":1,"name":"oData"},{"id":2,"name":"WebApi"}] 0 And it returns json, but my ajax callback goes to error and shows the following message: parseerror - [{"id":0,"name":".net"},{"id":1,"name":"oData"},{"id":2,"name":"WebApi"}] why it's not calling success callback function? – SCV May 06 '12 at 18:41
  • in your JsonpMediaTypeFormatter, what do you have for you SupportedMediaTypes ? – cecilphillip May 08 '12 at 14:39
  • also insert your jsonpmediaformatter at the top of the list, ie configuration.formatters.insert(0, new jsonpmediatypeformatter()) – gdp May 17 '12 at 12:31