jQuery的Ajax请求是块因为跨域 [英] jQuery ajax request being block because Cross-Origin

查看:223
本文介绍了jQuery的Ajax请求是块因为跨域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过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(响应); //服务器响应
    }

});
 

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
    }

});

这篇关于jQuery的Ajax请求是块因为跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆