gpt4 book ai didi

jQuery ajax 请求因跨源而被阻止

转载 作者:行者123 更新时间:2023-12-03 21:45:24 24 4
gpt4 key购买 nike

如何通过ajax从远程url获取内容?

jQuery ajax 请求因跨源而被阻止

控制台日志

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.dailymotion.com/embed/video/x28j5hv. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.dailymotion.com/embed/video/x28j5hv. (Reason: CORS request failed).

代码

$.ajax({
url: "http://www.dailymotion.com/embed/video/x28j5hv",
type:'GET',
contentType: "html",
crossDomain:true,
success: function(data){
//$('#content').html($(data).html());
var src = $(data).html();
alert(src);
return false;
}

最佳答案

尝试在 Ajax 调用中使用 JSONP。它将绕过同源策略。

http://learn.jquery.com/ajax/working-with-jsonp/

尝试示例

$.ajax({
url: "https://api.dailymotion.com/video/x28j5hv?fields=title",

dataType: "jsonp",
success: function( response ) {
console.log( response ); // server response
}

});

关于jQuery ajax 请求因跨源而被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31513450/

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