gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:14:11 24 4
gpt4 key购买 nike

请在下面找到我的代码以从 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' 因为它的 MIME 类型 ('application/json') 不可执行,并且启用了严格的 MIME 类型检查

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

在 chrome 开发人员工具的 Network 选项卡中,我从 confluence 获得了响应,但它在控制台或页面上打印的内容不同。

如果我使用 dataType:json,我会得到一个错误 XMLHttpRequest cannot load https://blog.xxxxx.com/rest/api/content?type=blogpost&spaceKey=xxxxx&expand=空间,body.view,版本,容器。请求的资源上不存在“Access-Control-Allow-Origin” header 。因此,不允许在 chrome 上访问源“https://localhost”

更新在 IIS 中为 json 添加 mime 类型 application/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 in confluence rest api ???

最佳答案

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。

No 'Access-Control-Allow-Origin' header is present on the requested resource

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

关于javascript - 拒绝从 '*' 执行脚本,因为它的 MIME 类型 ('application/json' ) 不可执行,并且严格的 MIME 类型 che,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26335594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com