不存在"Access-Control-Allow-Origin"标头 [英] No 'Access-Control-Allow-Origin' header is present
问题描述
我正在使用angular.js使用Cordova为移动设备制作一个ReSTful应用程序. 对于服务器端,我根据本教程使用的是slim框架.( http://www.angularcode.com/user-authentication-using-angularjs-php-mysql/)
但是当我将api文件夹上传到服务器时,会发生错误: 所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问源本地主机".响应的HTTP状态代码为404.
我已阅读有关添加"Access-Control-Allow-Origin"的信息 并根据此链接将其实现: http://help.slimframework.com/discussions/problems/810-no-subject
$app->get('/session', function() {
**$response->header('Access-Control-Allow-Origin', '*');**
$db = new DbHandler();
$session = $db->getSession();
$response["uid"] = $session['uid'];
$response["email"] = $session['email'];
$response["name"] = $session['name'];
echoResponse(200, $session);
});
也尝试过
$app->get('/session', function() {
**$session->header('Access-Control-Allow-Origin', '*');**
$db = new DbHandler();
$session = $db->getSession();
$response["uid"] = $session['uid'];
$response["email"] = $session['email'];
$response["name"] = $session['name'];
echoResponse(200, $session);
});
我仍然收到相同的错误.
->最后一行也是正确的吗? echoResponse(200, $session);
预先感谢您:)
更新
我在Firefox和chrome上使用了相同的文件,而chrome能够访问服务器上的文件. 另外,cordova应用程序之前已经访问了我服务器上的一个JSON文件,但现在无法正常工作.调用代码完全相同,即
.controller('ListController', ['$scope', '$http', function($scope, $http) {
$http.get('js/data.json').success(function(product_data) {
$scope.artists = product_data;
});
}])
某些网络浏览器不允许来自本地主机的CORS(或本地主机的任何别名,例如127.0.0.1).解决方案是使用非本地url来访问它.
一个可能有效的网址就是您的IP地址.
如果您不想使用您的IP地址(也许是因为它是每次连接时由您的路由器随机分配的),则可以通过修改/etc/hosts
(在Windows上为Windows\System32\Drivers\etc\hosts
)为自己分配一个伪造的域./p>
例如,将其添加到/etc/hosts
:
127.0.1.1 thismachine
然后,您可以通过在Web浏览器中键入http://thismachine
来访问Web服务器.
I am using angular.js to make a ReSTful app for mobile devices using cordova. for server side I am using slim framework.all according to this tutorial:(http://www.angularcode.com/user-authentication-using-angularjs-php-mysql/)
But when i uploaded api folder to server, error occurs : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost' is therefore not allowed access. The response had HTTP status code 404.
I have read about adding "Access-Control-Allow-Origin" and implemented it according to this link : http://help.slimframework.com/discussions/problems/810-no-subject
$app->get('/session', function() {
**$response->header('Access-Control-Allow-Origin', '*');**
$db = new DbHandler();
$session = $db->getSession();
$response["uid"] = $session['uid'];
$response["email"] = $session['email'];
$response["name"] = $session['name'];
echoResponse(200, $session);
});
also tried
$app->get('/session', function() {
**$session->header('Access-Control-Allow-Origin', '*');**
$db = new DbHandler();
$session = $db->getSession();
$response["uid"] = $session['uid'];
$response["email"] = $session['email'];
$response["name"] = $session['name'];
echoResponse(200, $session);
});
I am still receiving the same error.
--> Also is the last line correct ? echoResponse(200, $session);
Thank You in advance :)
update
I am using same file on firefox and chrome and chrome is able to access the file on server. Also one JSON file on my server was being accessed by cordova app earlier and now is not working. The calling code is exactly same ie
.controller('ListController', ['$scope', '$http', function($scope, $http) {
$http.get('js/data.json').success(function(product_data) {
$scope.artists = product_data;
});
}])
Some web browsers disallow CORS from localhost (or any alias for localhost such as 127.0.0.1). The solution is to access it using non-localhost url.
One such url that may work is your IP address.
If you prefer not to use your IP address (maybe because it's assigned randomly by your router each time you connect) then you can assign a fake domain to yourself by modifying /etc/hosts
(or Windows\System32\Drivers\etc\hosts
on Windows).
For example, add this to /etc/hosts
:
127.0.1.1 thismachine
Then you can access your web server by typing http://thismachine
in your web browser.
这篇关于不存在"Access-Control-Allow-Origin"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!