2014-09-06 122 views
4

agularJs控制器中的http.get請求在我的客戶端應用程序和api位於localhost時正常工作。當api被移動到服務器時,問題就出現了。在codeigniter中啓用cors(restserver by @chriskacerguis)

客戶端使用angularJs

$http.get('http://domain.com/api/spots/2/0').success(function(datas){ 
console.log(datas); 
}); 

日誌給出: 跨來源請求阻止:同源策略不允許在http://domain.com/api/spots/2/0讀取遠程資源。這可以通過將資源移動到相同的域或啓用CORS來解決。

我已經將這兩行我控制器構造

header("Access-Control-Allow-Origin: *"); 

header("Access-Control-Allow-Methods: GET"); 

還是同樣的錯誤。

回答

10

嘗試將OPTIONS添加到允許的方法。

header("Access-Control-Allow-Methods: GET, OPTIONS"); 
header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding"); 

並且一旦你設置了標題,當請求是方法'OPTIONS'時立即返回。

if ("OPTIONS" === $_SERVER['REQUEST_METHOD']) { 
    die(); 
} 

另請參閱this answer

Angular發送一個W3C CORS spec compliant preflight request,它會在實際嘗試之前檢查允許的正確方法。我個人認爲Mozilla Developer Network CORS page有助於理解CORS的流程。

+0

我已添加的選項。仍然沒有變化... 即時通訊使用[鏈接](https://github.com/chriskacerguis/codeigniter-restserver) codeigniter ..有什麼用這個? 並且我必須對angularJS進行任何更改嗎? – onerinas 2014-09-07 02:37:30

+0

好吧,我首先要仔細檢查你的瀏覽器開發工具,並看看網絡流量/控制檯日誌。確保你得到一個呼叫發送到api終點[或不],然後你的角碼不會拋出錯誤。 – Ross 2014-09-07 12:09:40

+0

謝謝,看起來應該是從事物的前端呼叫方。有一件事要做,就是在發生其他事情之前設置標題後,退出PHP腳本。因此,如果請求方法==='OPTIONS',那麼設置您的標題然後立即死掉。看到[這個答案](http://stackoverflow.com/questions/15602099/http-options-error-in-phil-sturgeons-codeigniter-restserver-and-backbone-js?rq=1)的更多細節(嘗試添加允許標題部分也從這個答案) – Ross 2014-09-07 15:28:23

0

如果您可以使用jQuery Ajax,然後在您的腳本中使用此行。

jQuery.support.cors = true; // force cross-site scripting (as of jQuery 1.5) 

它解決了這個問題對我來說,當我試圖張貼使用jQuery的Ajax從側邊欄桌面小工具的XAMPP PHP文件的一些字符串。

+0

在我的角應用程序或codeigniter? – onerinas 2014-09-07 02:43:00

+0

只有在使用jQuery庫時才能使用。 定義庫之後,將該行放入腳本文件中,或者將腳本標記放入HTML文件中。 – 2014-09-07 05:58:33

+0

之後,我必須通過jquery調用api? 如果是這樣,我最不喜歡這種方法,因爲使用jQuery和角度方法讀取某處可能會發生衝突(如果不是,請糾正我) – onerinas 2014-09-07 13:51:06

0

客戶端=> AngularJs(在本地主機與步兵運行:9000) 服務器端=> PHP(笨溶液)(在本地主機上運行:80)

只爲我工作的事情是添加此行到web服務控制器在我的PHP項目:

  /* 
      here you do whatever you do to build the $data 

     */ 

     //but just before returning the method data add this 

     header('Content-type: application/json'); 
     header("Access-Control-Allow-Origin: *"); 
     header("Access-Control-Allow-Methods: GET"); 
     header("Access-Control-Allow-Methods: GET, OPTIONS"); 
     header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding"); 
     echo json_encode($data, JSON_NUMERIC_CHECK); 
1

如果任何人面臨的問題,使在笨REST控制器rest.php文件CORS爲我工作。這也清楚地註釋中記錄這裏https://github.com/chriskacerguis/codeigniter-restserver/blob/master/application/config/rest.php

//Change this to TRUE 
$config['check_cors'] = TRUE; 

//No change here 
$config['allowed_cors_headers'] = [ 
    'Origin', 
    'X-Requested-With', 
    'Content-Type', 
    'Accept', 
    'Access-Control-Request-Method' 
]; 

//No change here 
$config['allowed_cors_methods'] = [ 
    'GET', 
    'POST', 
    'OPTIONS', 
    'PUT', 
    'PATCH', 
    'DELETE' 
]; 

//Set to TRUE to enable Cross-Origin Resource Sharing (CORS) from any source domain 
$config['allow_any_cors_domain'] = TRUE; 


//Used if $config['check_cors'] is set to TRUE and $config['allow_any_cors_domain'] is set to FALSE. 
//Set all the allowable domains within the array 
//e.g. $config['allowed_origins'] =['http://www.example.com','https://spa.example.com'] 

$config['allowed_cors_origins'] = []; 
0

我已經添加了下面的構造在我的控制器類

public function __construct($config = 'rest') 
{ 
    header('Access-Control-Allow-Origin: *'); 
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE"); 
    parent::__construct(); 
}