jQuery ajax 请求被阻止,因为跨域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31513450/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:48:32  来源:igfitidea点击:

jQuery ajax request being block because Cross-Origin

jqueryajaxcross-domain

提问by Arbab Rasheed

How to get content from remote url via ajax?

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

jQuery ajax request being block because Cross-Origin

jQuery ajax 请求被阻止,因为跨域

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

跨域请求被阻止:同源策略不允许读取http://www.dailymotion.com/embed/video/x28j5hv 上的远程资源。(原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。

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

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

回答by Hassaan

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

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

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

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
    }

});

回答by HaukurHaf

There is nothing you can do on your end (client side). You can not enable crossDomain calls yourself, the source (dailymotion.com) needs to have COORS enabled for this to work.

在您的一端(客户端),您无能为力。您不能自己启用跨域调用,源 (dailymotion.com) 需要启用 COORS 才能使其工作。

The only thing you can really do is to create a server side proxy script which does this for you. Are you using any server side scripts in your project? PHP, Python, ASP.NET etc? If so, you could create a server side "proxy" script which makes the HTTP call to dailymotion and returns the response. Then you call that script from your Javascript code, since that server side script is on the same domain as your script code, COORS will not be a problem.

您真正能做的唯一一件事就是创建一个服务器端代理脚本来为您执行此操作。您是否在项目中使用任何服务器端脚本?PHP、Python、ASP.NET 等?如果是这样,您可以创建一个服务器端“代理”脚本,该脚本对 Dailymotion 进行 HTTP 调用并返回响应。然后您从您的 Javascript 代码调用该脚本,因为该服务器端脚本与您的脚本代码在同一个域中,所以 COORS 不会有问题。

回答by Ram Chander

Try with cURL request for cross-domain.

尝试使用 cURL 请求跨域。

If you are working through third party APIs or getting data through CROSS-DOMAIN, it is always recommended to use cURL script (server side) which is more secure.

如果您通过第三方 API 工作或通过 CROSS-DOMAIN 获取数据,则始终建议使用更安全的 cURL 脚本(服务器端)。

I always prefer cURL script.

我总是喜欢 cURL 脚本。