doucuo1642 2012-03-27 16:30
浏览 70
已采纳

使用My REST API的同源策略

I would like to know what solution I can use in order to have a REST api with js as the client and php as the server side of the api?
I have tried using iFrame, channel files and custom headers.
But none has worked.
- I would like to know how I should do it.
Thanks in advance.

Things I have tried already:
1)iFrame:

$(document).ready(function(){

$("#fff").contents().find("#rrr").submit();
})(jQuery);
-------------
<body>
  <p id="hello">Hello World</p>
  <iframe id="fff" src="#">
    hi
    <form id="rrr" action="http://mydomain/rest/rest.php" method="get">
      <input type="text" value="testy" name="check" />
    </form>
  </iframe>
</body>

-->returns nothing.

2) Channel file:
file on server(js):(conjs.js)

 function getDev(callback){

    jQuery.ajax({
        url: 'rest.php',
        type: 'GET',
        data: 'name=John', // or $('#myform').serializeArray()
        success: function(data) { 
            callback('Get completed '+data); 
        }
    });
}
------------------
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
  <script src="http://mydomain/rest/conjs.js"></script>
</head>
<body>
  <p id="hello">Hello World</p>
  <script type="text/javascript">
getDev(function (response) {
    alert(response);
});
</script>
</body>
</html>

-->returns nothing.

3)Php headers:

switch ($_SERVER['HTTP_ORIGIN']) {
    case 'http://jsbin.com': case 'https://jsbin.com':
    header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    header('Access-Control-Max-Age: 1000');
    header('Access-Control-Allow-Headers: Content-Type');
    break;
}

-->returns nothing.

None has worked so far.

  • 写回答

1条回答 默认 最新

  • duandunzhuo3234 2012-03-27 16:35
    关注

    Setting up a Reverse Proxy would help. A reverse proxy is used to get around the same origin policy by making it look like the request came from the same server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?