Disable Same Origin Policy in Mobile Safari

这一生的挚爱 提交于 2020-01-03 07:24:30

问题


I have an HTML5/JavaScript app that was originally written to run in certain cars. 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. 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. Is there any way to temporarily disable the Same Origin Policy on an iPad?


回答1:


I had the same problem with a sencha app. I resolved by setting a base path to my javascript ajax calls, example:

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

My problem was that the from mobile I get access only with IP but ajax call were point to localhost.

Hope this trick helps.




回答2:


You need to run a web server, not the file protocol.




回答3:


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.




回答4:


It can be possible in Javascript if you use an ajax call to a public proxy which basically removes the same origin header. Or you could write a php curl get page where you make the call to using ajax. For code on this check this blogpost:

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




回答5:


Try to use JSONP in your ajax call. It will bypass the Same Origin Policy.

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



来源:https://stackoverflow.com/questions/12150320/disable-same-origin-policy-in-mobile-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!