简体   繁体   中英

JSONP script returning MIME Type error

Hello I'm currently working on a script that must extract information from a third party feed which returns a json file. I am not able to use CORS since I do not have server access, so based on desk research I was informed to use JSONP. I am able to see the callback (response) in chrome network's tab but I can't read the file in the chrome log. The point is that when I execute the following code I get the Error Message below. I can't turn off mime type checking. I've tried to have a look on other questions but couldn't find anything similar. What should be done?

"Refused to execute script from ' https://siteurl.com/json=jsonp&callback=jQuery321030035432758818903_1501098778362&_=1501098778363 ' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled. "

<script src="jquery.js"></script>
<script>
        $.ajax({
            type: 'GET',
            url: 'siteurl.com/json?callback=jsonp',
            dataType: 'jsonp',
            xhrFields: {
                withCredentials: false
            },
            headers: {
                "Accept" : "application/json; charset=utf-8",
                "Content-Type": "application/javascript; charset=utf-8",
                "Access-Control-Allow-Origin" : "*"
            },
            success: function (result) {
                console.log(result);
            },
            error: function (xhr, errorText) {
                console.log('Error ' + xhr.responseText);
            }
        }); 
</script>

Take a look at the network tab:

Network tab view

JSONP is not JSON! JSONP is a JavaScript program consisting of a function call with one argument.

The correct Content-Type is application/javascript .


I am not able to use CORS since I do not have server access, so based on desk research I was informed to use JSONP.

You can only use JSONP if the site provides JSONP. (These days they should use CORS instead, it is better in every way). Just slapping callback in a query string will not magically force a site to provide JSONP and break the same origin policy for you. The site has to explicitly expose the data to other sites.

It's a problem with Content-Type.

Content-Type : application/json is correct for JSON but its not for JSONP.

Content-Type : application/javascript is for JSONP.

Please check this : What is the correct JSON content type?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM