Javascript 拒绝从 '*' 执行脚本,因为它的 MIME 类型('application/json')不可执行,并且严格的 MIME 类型 che
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26335594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting Refused to execute script from '*' because its MIME type ('application/json') is not executable, and strict MIME type che
提问by Valay
Please find below my code to get response from 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>
I referred thisand thisas reference but it did not resolve my isse. I am getting error on console 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.
我把这个和这个作为参考,但它没有解决我的问题。我在控制台上收到错误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。
I tried with type:post, dataType:jsonand dataType:jsonpwith jsonp: jsonp-callback. None of these worked for me.
我试着用type:post,dataType:json并dataType:jsonp用jsonp: jsonp-callback。这些都不适合我。
In Networktab of chrome developer tools I am getting resposne from confluencebut it does not print the same on console or on page.
在Networkchrome 开发人员工具的选项卡中,我收到了 resposne,confluence但它没有在控制台或页面上打印相同的内容。
If I use dataType:json, I am getting an error 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 accesson chrome.
如果我使用dataType:json,我会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在 chrome 上遇到错误。
UpdateAdding mime type application/jsonfor jsonin IIS does not work.
更新添加MIME类型application/json为jsonIIS中不起作用。
Updated code
更新代码
$.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);
}
});
Still getting the same error.
仍然得到同样的错误。
Response Body
响应体
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
How do I reolve the issue of MIME type ('application/json') is not executable, and strict MIME type checking is enabledin confluence rest api???
我如何解决MIME type ('application/json') is not executable, and strict MIME type checking is enabledin的问题confluence rest api???
回答by Quentin
https://blog.xxxxx.com/rest/api/content?type=blogpost&spaceKey=xxxxx&expand=space,body.view,version,containeris returning JSON.
https://blog.xxxxx.com/rest/api/content?type=blogpost&spaceKey=xxxxx&expand=space,body.view,version,container正在返回 JSON。
You are telling jQuery to read it as JSONP.
您告诉 jQuery 将其读取为 JSONP。
JSON and JSONP are different.
JSON 和 JSONP 是不同的。
You need to either change the server to respond with JSONP or change the JavaScript to expect JSON.
您需要更改服务器以使用 JSONP 进行响应,或者更改 JavaScript 以期待 JSON。
No 'Access-Control-Allow-Origin' header is present on the requested resource
请求的资源上不存在“Access-Control-Allow-Origin”标头
If you change the client to expect JSON, then you also need to change the server (blog.xxxxx.com) to provide the CORS headers that give the browser permission to ignore the Same Origin Policy.
如果您将客户端更改为期望 JSON,那么您还需要更改服务器 ( blog.xxxxx.com) 以提供 CORS 标头,允许浏览器忽略同源策略。

