2017-02-25 6 views
1

私のcodeigniterアプリケーションでAJAXリクエストを処理しようとしています。 は私のCodeIgniterのコントローラ機能の終わりに、私はここでコーディネータでCORSが動作しないようにする

public somefunction(){ 

$this->output->set_header('Access-Control-Allow-Origin: *'); 
$this->output->set_header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); 
$this->output->set_content_type('application/json'); 
// plan contains array 
return $this->output->set_output(json_encode($plan)); 
} 

Normal get request works via server to server, but AJax calls shows the error. XMLHttpRequest cannot load localhost:8888. Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

を追加しました私は郵便配達でそれをチェックして、私は、データを取得するので、Ajaxは、

self.information = function() { 
     $.ajax({ 
      type: 'GET', 
      url: '', 
      contentType: 'application/json; charset=utf-8' 
     }) 
     .done(function(result) { 
     console.log(result); 
     }) 
     .fail(function(xhr, status, error) { 
      alert(error); 
     }) 
     .always(function(data){ 
     }); 
    } 

URL作品を呼び出しています戻ってきた。だから問題はない。

+0

http://stackoverflow.com/questions/20511988/set-cross-domain-in-codeigniter要求のAJAXコンテンツタイプを変更することを示唆しています。おそらく試してみる価値があります。ところで、私が試した最初のものの一つ... – apokryfos

+0

@apokryfos? – FaF

+0

が本当にそこ 'ヘッダ(' 'set_header'で一部であるか、またはそれはコピー/ペーストタイプミスですだっ – apokryfos

答えて

0

私は残念ながら、私はと十分に慣れていないんだ何私のために働いたことは

public function index_options() { 
    return $this->response(NULL, REST_Controller::HTTP_OK); 
} 

この

public function __construct(){ 
    parent::__construct(); 
    header('Access-Control-Allow-Origin: *'); 
    header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method"); 
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); 
} 

に私のコンストラクタを更新index_optionsメソッドを定義して、今日、同じ問題に出くわしましたなぜこれが私のために働いたのかを説明するアクセス制御。お役に立てれば。

+0

こんにちはアワドが、私は)(私のsomefunctionに追加します?どのように全部一緒に作業するのですか? – FaF

+0

@Farhanaは、あなたのsomefunction関数と同じようにindex_options関数を定義するだけで、ブラウザがプリフライトOPTIONS要求について不平を言わなくなります。 t、ヘッダーの変更をコンストラクタに移動 –

+0

index_options関数は何も呼び出されません。どのようにロードされますか?またはciが自動的にロードしますか? – FaF

関連する問題