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 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败