[英]CORS error when POST data in AngularJS & CodeIgniter3
我正在使用AngularJS 1.3和CodeIgniter 3.0。
我成功從localhost中的php獲取數據。 但是我有錯誤“跨源請求被阻止”。 我不知道,但是經過搜索,請幫助我。
Java腳本
var module = angular.module('app', ['onsen']);
module.config(function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
module.controller('MasterController', function($scope, $http) {
$scope.doLogin = function() {
var postData = {"email": "aaa@bbb.ccc", "password": "pass"};
var url = 'http://example/test?callback=JSON_CALLBACK';
//This section is Error
$http.post(url, postData, {withCredentials: true})
.success(function(data, status, headers, config) {
//
}).error(function(data, status, headers, config) {
//
});
//This section is Success
$http.get(url).success(function(data, status, headers, config) {
//
}).error(function(data, status, headers, config) {
//
});
};
});
我嘗試了“ $ http({url〜})”,但是結果是一樣的。
的PHP
public function index() {
$input_data = json_decode(trim(file_get_contents('php://input')), true);
$this->output
->set_header("Access-Control-Allow-Origin: *")
->set_header("Access-Control-Expose-Headers: Access-Control-Allow-Origin")
->set_content_type('application/json')
->set_output(json_encode($input_data));
}
嘗試使用.htaccess
允許CORS。 將其放在您的服務器.htaccess文件中
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
即使是訪問不同的端口,瀏覽器也將其視為跨域操作,有關如何啟用CORS的最佳指南,請參見http://enable-cors.org/。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.