简体   繁体   English

在Mobile Safari中禁用同源策略

[英]Disable Same Origin Policy in Mobile Safari

I have an HTML5/JavaScript app that was originally written to run in certain cars. 我有一个HTML5 / JavaScript应用程序,最初编写为在某些汽车中运行。 Basically, I need to set up my app to run in the browser for a simple demo to a customer. 基本上,我需要将我的应用程序设置为在浏览器中运行,以便向客户进行简单的演示。

I'm using jQuery .ajax which is causing problems due to the Same Origin Policy. 我正在使用jQuery .ajax,由于同源策略导致问题。 I have found plenty of ways to disable this in desktop browsers, but not mobile ones. 我发现有很多方法可以在桌面浏览器中禁用它,但不能在移动浏览器中禁用它。

My goal is to demo the app on an iPad in Mobile Safari. 我的目标是在Mobile Safari中在iPad上演示应用程序。 Is there any way to temporarily disable the Same Origin Policy on an iPad? 有没有办法暂时禁用iPad上的同源策略?

I had the same problem with a sencha app. 我对sencha应用程序有同样的问题。 I resolved by setting a base path to my javascript ajax calls, example: 我通过设置我的javascript ajax调用的基本路径来解决,例如:

var BASEPATH = 'http://192.168.1.200/myapp';

$.ajax({
  url: BASEPATH+'/someaction'
});

And from the mobile I access it with http://192.168.1.200/myapp 从移动设备我访问http://192.168.1.200/myapp

My problem was that the from mobile I get access only with IP but ajax call were point to localhost. 我的问题是,从移动设备我只能访问IP,但ajax调用指向localhost。

Hope this trick helps. 希望这个技巧有所帮助

您需要运行Web服务器,而不是文件协议。

Basically, you need a header. 基本上,你需要一个标题。

Put this code at the top of the page you want to send cross domain requests to. 将此代码放在要向其发送跨域请求的页面顶部。

<?php header("Access-Control-Allow-Origin: *"); ?>

Be careful with the *, as this allows any website to send requests to the page from which that header is sent from. 小心*,因为这允许任何网站向发送该标头的页面发送请求。

The * can be replaced with domains, such as example.com, example.net. *可以用域替换,例如example.com,example.net。

It can be possible in Javascript if you use an ajax call to a public proxy which basically removes the same origin header. 如果你使用ajax调用公共代理基本上删除相同的源头,则可以在Javascript中使用。 Or you could write a php curl get page where you make the call to using ajax. 或者你可以编写一个php curl get页面来调用使用ajax。 For code on this check this blogpost: 有关此代码,请查看此博文:

http://thewebtimes.tumblr.com/post/90549614884/access-forbidden-webpages-with-javascript http://thewebtimes.tumblr.com/post/90549614884/access-forbidden-webpages-with-javascript

Try to use JSONP in your ajax call. 尝试在ajax调用中使用JSONP。 It will bypass the Same Origin Policy. 它将绕过同源政策。

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

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

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