2013-10-02 59 views
0

你好我試圖ajax跨域調用,但有問題的標題。我跟着很多教程,但我不能讓它工作。不能得到CORS在鉻和火狐工作

這是我有沒有走多遠:

<html> 
<head> 
<meta http-equiv="Access-Control-Allow-Origin" content="*"> 
</head> 
<body> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" ></script> 

<script> 

    jQuery.support.cors = true; 

    $(document).ready(function() { 

     //this would be done in a common script file if you are going to 
     //make a lot of these calls 
     $.ajaxSetup({ type: 'POST', dataType: 'json', contentType: 'application/json', data: {} }); 

     $('#btn').click(function() { 
      //call the web service with the button is clicked 
      $.ajax({ url: 'url_external', 
       data: '{ "some_data:data" }', //ensure the data is enclosed in a quote 
          success: function (data) { 
       alert('here...success'); 
      }, 
      complete: function (data) { 
       alert('here'); 
      } 
      }); 

      //return false to avoid a postback 
      return false; 
     }); 

    }); 
</script> 


<div> 
    <button id='btn'>Load It</button> 
    <div id='sayit'></div> 
</div> 
</body> 
</html> 

而在IE9這只是工作。我錯過了什麼嗎?在Mozilla Firefox和谷歌Chrome瀏覽器存在問題,選項和405不允許的方法

+0

問題是您的服務器沒有處理OPTIONS請求。 –

+0

以及如何處理它? –

+1

https://developer.mozilla.org/zh-CN/docs/HTTP/Access_control_CORS –

回答

0

這所有的HTTP響應添加'url_external'服務器上:

<?php 

header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE'); 
header('Access-Control-Allow-Headers', 'Content-Type'); 

這使您url_externalCORS Ajax請求服務器。

你可能要微調,這...

header('Access-Control-Allow-Origin: http://yourdomain.com'); 

...以只允許從您的域的唯一CORS請求。

我發現有關CORS的最佳信息:Using CORS

相關問題