jQuery的Ajax请求是块因为跨域求是、jQuery、Ajax

2023-09-11 00:42:04 作者:勉强可爱

如何通过AJAX从远程URL的内容?

jQuery的Ajax请求是块因为跨域

控制台登录

  

跨域请求阻止:同源策略不允许的阅读   在 http://www.dailymotion.com/embed/video/x28j5hv 远程资源。   (原因:CORS头访问控制 - 允许 - 原产地失踪)

     

跨域请求阻止:同源策略不允许的阅读   在 http://www.dailymotion.com/embed/video/x28j5hv 远程资源。   (原因:CORS请求失败)

code

  $。阿贾克斯({
网址:http://www.dailymotion.com/embed/video/x28j5hv
键入:GET,
的contentType:HTML,
跨域:真正的,
成功:功能(数据){
   // $('#内容)HTML($(数据)的.html());
   变种的src = $(数据)的.html();
    警报(SRC);
    返回false;
}
 

解决方案

尝试使用 JSONP 在你的Ajax调用。它会绕过同源策略。

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

尝试例如

  $。阿贾克斯({
    网址:https://api.dailymotion.com/video/x28j5hv?fields=title

    数据类型:JSONP
    成功:函数(响应){
        的console.log(响应); //服务器响应
    }

});
 
jQuery Ajax

How to get content from remote url via ajax?

jQuery ajax request being block because Cross-Origin

Console Log

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).

Code

$.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;
}

解决方案

Try to use JSONP in your Ajax call. It will bypass the Same Origin Policy.

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

Try example

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

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

});