简体   繁体   English

没有 CORS 或 JSONP 的跨域请求

[英]Cross-Domain Request without CORS or JSONP

I know this question has been asked before, but none of the answers have been working for me!我知道以前有人问过这个问题,但没有一个答案对我有用! I am doing a school project and I would like to get the HTML (to parse it for my project) returned by the dynamic schedule files on my schools server.我正在做一个学校项目,我想获取我的学校服务器上的动态时间表文件返回的 HTML(为我的项目解析它)。

The page I would like the HTML of is: https://telaris.wlu.ca/ssb_prod/bwckschd.p_disp_dyn_sched我想要的 HTML 页面是: https : //telaris.wlu.ca/ssb_prod/bwckschd.p_disp_dyn_sched

I think that CORS is not enabled for the school server files and I do not know if it supports JSONP...我认为学校服务器文件没有启用CORS,我不知道它是否支持JSONP ...

How do I set up the cross-domain request to get the HTML from this page?如何设置跨域请求以从此页面获取 HTML?

I have tried:我试过了:

$.ajax({
    type:'POST',
    url: 'https://telaris.wlu.ca/ssb_prod/bwckschd.p_disp_dyn_sched',
    headers: {
      'Access-Control-Allow-Origin': '*'
   },
   contentType: 'text/html',
   crossDomain:true
}).done(function( data ) {

});

and I get the error:我得到错误:

XMLHttpRequest cannot load https://telaris.wlu.ca/ssb_prod/bwckschd.p_disp_dyn_sched . XMLHttpRequest 无法加载https://telaris.wlu.ca/ssb_prod/bwckschd.p_disp_dyn_sched No 'Access-Control-Allow-Origin' header is present on the requested resource.请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'null' is therefore not allowed access.因此,不允许访问 Origin 'null'。 The response had HTTP status code 501.响应具有 HTTP 状态代码 501。

When I add:当我添加:

dataType:'jsonp'

I get the error:我收到错误:

GET https://telaris.wlu.ca/ssb_prod/bwckschd.p_disp_dyn_sched?callback=jQuery21108736664191819727_1416964243449&_=1416964243450 400 (Bad Request) jquery.min.js:4send jquery.min.js:4n.extend.ajax jquery.min.js:4(anonymous function)获取https://telaris.wlu.ca/ssb_prod/bwckschd.p_disp_dyn_sched?callback=jQuery21108736664191819727_1416964243449&_=1416964243450 400 400 400 96964243450 400 js.query.jsmin.jsn.query.jsmin.jsmin.js. :4(匿名函数)

Any help is greatly appreciated!任何帮助是极大的赞赏!

Browsers enforce "same-origin" access control unless the site explicitly allows cross origin requests (either via CORS or JSONP).浏览器强制执行“同源”访问控制,除非站点明确允许跨源请求(通过 CORS 或 JSONP)。 So, if the site you are trying to access does not allow cross origin requests, then you cannot get the data directly from the site using only a browser.因此,如果您尝试访问的站点不允许跨源请求,则您无法仅使用浏览器直接从站点获取数据。 The browser is enforcing the same origin restrictions requested by the target site.浏览器正在执行目标站点请求的同源限制。

This is NOT security for a server at all as there are many ways around it.这根本不是服务器的安全性,因为有很多方法可以解决它。 It only applies to one specific type of access from a browser (though that one specific type of access protection is useful).它仅适用于来自浏览器的一种特定类型的访问(尽管一种特定类型的访问保护很有用)。

This means to get the data into a browser you will need to use some sort of third party agent (other than the browser) that can get the data for you.这意味着要将数据输入浏览器,您需要使用某种第三方代理(浏览器除外)来为您获取数据。 The two most common ways of doing that are:两种最常见的方法是:

  1. Your own server.你自己的服务器。 You make a request of your own server to get some content from some other server.您向自己的服务器发出请求以从其他服务器获取某些内容。 Your server then fetches the data from the other server and returns it to you in the browser.然后您的服务器从另一台服务器获取数据并在浏览器中将其返回给您。

  2. A proxy server.代理服务器。 There are some preconfigured proxy servers that are built just for doing what is described in option #1.有一些预配置的代理服务器专为执行选项 #1 中描述的操作而构建。 You can either use a proxy service or install your own proxy server to do this for you or configure your own web server to have this capability.您可以使用代理服务或安装您自己的代理服务器来为您执行此操作,或者配置您自己的 Web 服务器以具有此功能。

So, you can't bypass cross origin restrictions from a cooperating browser.因此,您无法绕过协作浏览器的跨源限制。 But, you can bypass them from a server.但是,您可以从服务器绕过它们。 This is because CORs restrictions are implemented only in the browser.这是因为 COR 限制仅在浏览器中实现。 They aren't a server-enforced restriction.它们不是服务器强制的限制。 The browser asks the target server what CORs policies are in play and enforces them in the browser only.浏览器询问目标服务器正在执行哪些 COR 策略,并仅在浏览器中执行这些策略。 Some other server making a request to that server does not need to pay any attention to CORs policies.向该服务器发出请求的某些其他服务器不需要关注 COR 策略。

I know how frustrating it will be when technical limitations makes life more harder.我知道当技术限制让生活变得更加艰难时会有多么令人沮丧。

For the situation like this, you can try the Javascript library: Xdomain遇到这种情况,可以试试Javascript库: Xdomain
You can check the reference article for the same here: http://hayageek.com/cross-domain-ajax-jquery-without-cors/您可以在此处查看相同的参考文章: http : //hayageek.com/cross-domain-ajax-jquery-without-cors/
The YCombinator discussion may help, if you have issues: https://news.ycombinator.com/item?id=8023844如果您有问题,YCombinator 讨论可能会有所帮助: https ://news.ycombinator.com/item?id=8023844

Other way is, use your server to get the HTML page, and push to your application as and when required.另一种方法是,使用您的服务器获取 HTML 页面,并在需要时推送到您的应用程序。 Something like this.像这样的东西。 Assume you have added one iframe as 'my_external_content.php' .假设您添加了一个 iframe 作为'my_external_content.php' The code should be代码应该是

<?php

    $externalURL = "http://otherdomain.com";

    // requires fopen wrappers
    $externalData = file_get_contents($externalURL);

    echo $externalData;
?>

Please refer the other question on SO for some reference: Ways to Circumvent Same Origin Policy请参阅 SO 上的其他问题以获取一些参考: 绕过同源政策的方法

Hope this helps your or others who are in same situation.希望这可以帮助您或其他处于相同情况的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM