Search code examples
javascriptjqueryajaxjsonjsonp

jQuery AJAX - JSONP convert text to JSON


Expected Duplication 1

Expected Duplication 2

I am searching a proper explanation about JSONP in various question but still not able to find.

For example,

I wanted to do a cross domain request using JSONP and the format of the response is text/plain. Is there any way to override or re-format the response? i.e. parsing the text/plain response with dataType: "jsonp"

I always get the error,

Resource interpreted as Script but transferred with MIME type text/plain: "Some_url" Uncaught SyntaxError: Unexpected identifier

Can someone explain me that the RestAPI should always return JSON formatted result?


Solution

  • I wanted to do a cross domain request using JSONP and the format of the response is text/plain.

    You need to understand that JSONP technique utilizes <script> tag to load resource. Response should be valid javascript code then, which looks something like this:

    callback_function({"some": "data", "in": "JSON format"});
    

    So the answer to your question is no, you can't respond with HTML and then transform it, it will just generate the error you have already seen.