0

I use the following method to create an HttpResponseMessage object and return it via my Web API project:

public HttpResponseMessage CreateHttpResponseMessage(string message, HttpStatusCode httpStatusCode)
{
    var stringContent = new StringContent(message, new UTF8Encoding(), "application/javascript");
    return new HttpResponseMessage(httpStatusCode)
        {
            Content = stringContent
        };
}

I'm using this to return a jsonp payload however it keeps returning with content type text/plain and not application/javascript. This is causing the browser console to log this warning:

Resource interpreted as Script but transferred with MIME type text/plain

The message I'm returning isn't null or empty and therefore I'm confused as to why it's behaving this way.

1 Answer 1

1

You cannot return JSONP as plain text as you are trying to return.

You need to configure your Web API to use a JSONP MediaTypeFormatter. A such formatter is offered in WebApiContrib.

Install-Package WebApiContrib.Formatting.Jsonp

Add to Application_Start:

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

Resources:
http://stevenhollidge.blogspot.in/2013/03/how-to-return-jsonp-from-webapi.html
http://www.codeproject.com/Tips/631685/JSONP-in-ASP-NET-Web-API-Quick-Get-Started

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.