我试图在我的Node.js应用程序中支持使用Express.js Web框架的CORS.我已经阅读了有关如何处理此问题的Google小组讨论,并阅读了一些有关CORS如何工作的文章.首先,我这样做了(代码是用CoffeeScript语法编写的):
app.options "*", (req, res) ->
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Credentials', true
# try: 'POST, GET, PUT, DELETE, OPTIONS'
res.header 'Access-Control-Allow-Methods', 'GET, OPTIONS'
# try: 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'
res.header 'Access-Control-Allow-Headers', 'Content-Type'
# ...
Run Code Online (Sandbox Code Playgroud)
它似乎不起作用.好像我的浏览器(Chrome)没有发送初始OPTIONS请求.当我刚刚更新资源块时,我需要将跨源GET请求提交到:
app.get "/somethingelse", (req, res) ->
# ...
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Credentials', true
res.header 'Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS'
res.header 'Access-Control-Allow-Headers', 'Content-Type'
# ...
Run Code Online (Sandbox Code Playgroud)
它有效(在Chrome中).这也适用于Safari.
我读过......
在实现CORS的浏览器中,每个跨源GET或POST请求前面都有一个OPTIONS请求,用于检查GET或POST是否正常.
所以我的主要问题是,为什么在我的案例中似乎没有发生这种情况?为什么我的app.options块没有被调用?为什么我需要在主app.get块中设置标题?
我<div id="content">想要将url:http://vnexpress.net内容加载到我的代码中:
<html>
<head>
<script type="text/javascript">
$(document).ready(function(){
$("#content").attr("src","http://vnexpress.net");
})
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我不想使用Iframe
所以基本上我使用42matters.com APP市场API从谷歌游戏商店获取应用程序详细信息或信息,一切正常,我收到了JSON回复,但当我周末假期后回到办公室时,这个相当奇怪的错误来了,没有任何东西被退回.
我使用了$ .getJSON函数,如:
var packageID = 'com.whatsapp';
$.getJSON('https://42matters.com/api/1/apps/lookup.json?p='+packageID+'&access_token=accesstoken1234')
.done(function(appDetails) {
$('#logo').html(JSON.stringify(appDetails));
});
Run Code Online (Sandbox Code Playgroud)
如上所述,这是返回数据,我能够相应地改变一切,但现在它无缘无故地给了我这个错误
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://42matters.com/api/1/apps/lookup.json?p=com.whatsapp&access_token=accesstoken1234. This can be fixed by moving the resource to the same domain or enabling CORS
Run Code Online (Sandbox Code Playgroud)
我已经使用PHP启用了CORS,没有任何问题,尝试更改apache的conf文件以启用CORS,但服务不会重启,所以我被卡住了.
还有一件事,当我在浏览器中手动输入上述链接时,它确实提供了所需的结果.请帮我解决一下
我有一个移动应用程序,它使用API通过登录表单对用户进行身份验证.
这一直在今天工作正常..现在,当我尝试登录时,我在控制台日志中收到以下消息:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myapp.local/myAppApi/V1/appLogin.
This can be fixed by moving the resource to the same domain or enabling CORS.
Run Code Online (Sandbox Code Playgroud)
显然我需要启用CORS来读取消息,在myApiController.php中,我在Yii应用程序中有以下代码,我认为应该这样做:
protected function _renderJSON($status = 200)
{
$statusCodeMessage = $this->_getStatusCodeMessage($status);
header("HTTP/1.1 {$status} {$statusCodeMessage}");
// allow for Cross Origin Resource Sharing
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Authorization");
header('Content-type: application/json');
echo CJSON::encode($this->jsonArray);
foreach (Yii::app()->log->routes as $route) {
if ($route instanceof CWebLogRoute) {
$route->enabled = false; // disable any …Run Code Online (Sandbox Code Playgroud) 我试图使用angular $ resourses访问在localhost中运行的api,chrome console给出了错误说ERR_INSECURE_RESPONSE.
我尝试在Chrome中禁用网络安全性.还是一样的错误.这是我使用的角度工厂.如何绕过此错误并测试我的应用.
ImpactPortal.factory('apiFactory', function ($resource) {
return $resource('https://localhost:8443/mifosng-provider/api/v1/client_impact_portal', {}, {
query: {
method: 'GET',
params: {},
isArray: true
}
})
});
Run Code Online (Sandbox Code Playgroud)