拒绝从'*'执行脚本,因为它的MIME类型('application / json')不可执行,并且严格的MIME类型为che

时间:2014-10-13 08:20:15

标签: javascript ajax json rest confluence-rest-api

请在下面找到我的代码,以便从confluence rest api获得回复:

<script type="text/javascript" src="Scripts/jquery.min.js"></script>
<script>
    $.ajax({
        type: "GET",
        url: "https://blog.xxxxx.com/rest/api/content?type=blogpost&spaceKey=xxxxx&expand=space,body.view,version,container",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        jsonp: 'jsonp-callback',
        async: false,
        success: function (result) {
            console.log(result);
        },
        error: function (xhr, errorText) {
            console.log('Error ' + xhr.responseText);
        }
    });
</script>

我提到this and this作为参考,但它没有解决我的问题。我在控制台Refused to execute script from 'https://blog.xxxxx.com/rest/api/content?type=blogpost&spaceKey=xxxxx&…d=space,body.view,version,container&callback=jsonpCallback&_=1413187692508' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled上收到错误。

我尝试使用type:postdataType:jsondataType:jsonp jsonp: jsonp-callback。这些都不适合我。

在Chrome开发者工具的Network标签中,我从confluence获得了转发,但它在控制台或页面上不会打印相同内容。

如果我使用dataType:json,我在Chrome上收到错误XMLHttpRequest cannot load https://blog.xxxxx.com/rest/api/content?type=blogpost&spaceKey=xxxxx&expand=space,body.view,version,container. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost' is therefore not allowed access

更新 在IIS中为application/json添加mime类型json不起作用。

更新了代码

$.ajax({
            type: 'GET',            
            url: 'https://blog.xxxxx.com/rest/api/content?type=blogpost&spaceKey=xxxxx&expand=space,body.view,version,container',   
            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) {
                $('#blog').html(result);
            },
            error: function (xhr, errorText) {
                console.log('Error ' + xhr.responseText);
            }
        });

仍然遇到同样的错误。

回复正文

results: [{id:3342352, type:blogpost, title:The stocks that are set to fly (or crash),…},…]
0: {id:3342352, type:blogpost, title:The stocks that are set to fly (or crash),…}
1: {id:3833861, type:blogpost, title:Before earnings season, it's downgrade season,…}
2: {id:3833876, type:blogpost, title:Petrobras - what goes up, must come down,…}
3: {id:3833882, type:blogpost, title:Fishing for Income in the FTSE 100,…}
4: {id:4489219, type:blogpost, title:A Ray of Light Among the Gathering German Gloom,…}
5: {id:4489234, type:blogpost, title:Insider trading falls as buybacks dominate share prices,…}
6: {id:4489241, type:blogpost, title:El Clasico: Nike vs Adidas,…}
7: {id:4489248, type:blogpost, title:Dollar uncertainty exposes investors' complacency,…}
8: {id:4489254, type:blogpost, title:Worst yet to come for the Australian miners,…}
9: {id:4489258, type:blogpost, title:Using Aggregate List Views to Find Lurking Risks,…}
size: 10
start: 0

如何在MIME type ('application/json') is not executable, and strict MIME type checking is enabled中解决confluence rest api的问题???

2 个答案:

答案 0 :(得分:3)

https://blog.xxxxx.com/rest/api/content?type=blogpost&spaceKey=xxxxx&expand=space,body.view,version,container正在返回JSON。

您告诉jQuery将其读作JSONP。

JSON和JSONP不同。

您需要更改服务器以使用JSONP进行响应,或者将JavaScript更改为期望JSON。

  

否&#39;访问控制 - 允许 - 来源&#39;标头出现在请求的资源上

如果您将客户端更改为期望JSON,那么您还需要更改服务器(blog.xxxxx.com)以提供CORS标头,使浏览器可以忽略同源策略。

答案 1 :(得分:1)

我在Docs中看到了这一点:

仅适用于GET。返回的内容类型 必须是application / javascript。

同时检查启用JSONP