javascript 使用 .ajax() 加载远程 URL - 不工作

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

Using .ajax() to load remote URL - not working

javascriptjqueryajax

提问by vipin katiyar

I'm trying to load a remote URL (or rather just test to see if a remote page exists).

我正在尝试加载远程 URL(或者只是测试以查看远程页面是否存在)。

This works just fine:

这工作得很好:

$(function() {     
    $.ajax({
        url:'localtest.html',
        type: 'html',
        success: function(content,code) {
            alert(code);
            $('body').html(content);
        }
    });        
});

But swap in a remote URL and I get nothing:

但是交换一个远程 URL,我什么也没得到:

$(function() {     
    $.ajax({
        url:'http://www.google.com/',
        type: 'html',
        success: function(content,code) {
            alert(code);
            $('body').html(content);
        }
    });        
});

Is there any way to do that?

有没有办法做到这一点?

回答by u.k

AJAX doesn't support cross domain calls, for security reasons.

出于安全原因,AJAX 不支持跨域调用。

The traditional way around it is using jsonp

解决它的传统方法是使用jsonp

回答by devnull69

Browsers prevent Ajax from accessing resources cross domain (SOP = same origin policy). It will only work if the server is configured for "Access-Control-Allow-Origin" pointing to your domain (or * or similar).

浏览器阻止 Ajax 跨域访问资源(SOP = 同源策略)。仅当服务器配置为指向您的域(或 * 或类似)的“Access-Control-Allow-Origin”时,它才有效。

回答by Howard

This is because the browser won't allow cross-site requests unless the remote server explicitly allows it by sending an Access-Control-Allow-Origin header. If you just want to test existence, you might be able to load the URL in an image tag with an onload and onerror event. You won't be able to access the contents of the remote URL though; it's for security. Otherwise, for example, you could load facebook and read someone's wall without them knowing.

这是因为浏览器不允许跨站点请求,除非远程服务器通过发送 Access-Control-Allow-Origin 标头明确允许它。如果您只是想测试是否存在,您可以使用 onload 和 onerror 事件在图像标签中加载 URL。但是,您将无法访问远程 URL 的内容;是为了安全。否则,例如,您可以加载 facebook 并在他们不知情的情况下阅读某人的墙。

回答by paramjeet singh

For security reasons,AJAX doesn't support CORS(cross origin resource sharing).

出于安全原因,AJAX 不支持 CORS(跨源资源共享)。

回答by Benny Tjia

This is not a right ajax call. The type should have been 'GET' assuming you are doing a retrieval operation. What should be 'html' is the dataType attribute.

这不是一个正确的 ajax 调用。假设您正在执行检索操作,类型应该是“GET”。'html' 应该是 dataType 属性。

$.ajax({
        url:'http://www.google.com/',
        type: 'GET'
        dataType: 'html',
        success:function(content,code)
        {
            alert(code);
            $('body').html(content);
        }
        });        
    });

and also, as anybody has added, since you are making a call to google.com, you have to provide a jsonp callback, because of same origin policy..hope that helps.

而且,正如任何人所补充的那样,由于您正在拨打 google.com,因此您必须提供 jsonp 回调,因为同源策略..希望有所帮助。

回答by user2417120

Use attribute in AJAX call.

在 AJAX 调用中使用属性。

crossDomain: true

And do make sure meta data tag of html page where you are loading contents.

并确保您正在加载内容的 html 页面的元数据标记。