我正在写我的webApp,而且我正在使用AngularJS.在这个应用程序中,我创建了一个名为script.js的文件,并报告此代码:
var modulo = angular.module('progetto', ['ngRoute']);
// configure our routes
modulo.config(function ($routeProvider, $httpProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl: 'listaFilm.html',
controller: 'listaController'
})
// route for the description page
.when('/:phoneName', {
templateUrl: 'description.html',
controller: 'descriptionController'
});
$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
});
modulo.controller('listaController', function ($scope, $http) {
$http.get('https://api.getevents.co/event?&lat=41.904196&lng=12.465974').success(function (data) {
$scope.names = data;
}).
error(function (data, status) {
$scope.names = "Request failed";
});
});
Run Code Online (Sandbox Code Playgroud)
使用此代码,我按照RESTful原则调用API.当我运行代码时,我遇到了这个问题:
XMLHttpRequest无法加载https://api.getevents.co请求的资源上没有"Access-Control-Allow-Origin"标头.原产地" 的http://本地主机:8383 "因此不允许访问.
在网上阅读我明白我有一个叫CORS的问题...我已经尝试了几个解决方案,但我没有解决问题.
我该如何解决这个问题?
我必须添加什么代码来修复它?
我正在尝试为我的angular2应用添加基本身份验证.
public login() {
// Set basic auth headers
this.defaultHeaders.set('Authorization', 'Basic ' + btoa(this.username + ':' + this.password));
console.log('username', this.username)
console.log('password', this.password)
console.log(this.defaultHeaders)
// rest is copy paste from monbanquetapiservice
const path = this.basePath + '/api/v1/development/order';
let req = this.http.get(path, { headers: this.defaultHeaders });
req.subscribe(
_ => { },
err => this.onError(err)
);
}
Run Code Online (Sandbox Code Playgroud)
我期望看到的是带有Authorization
我放置的标题的GET请求.
但我看到的首先是带有此标题的OPTIONS:
OPTIONS /api/v1/development/order HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X …
Run Code Online (Sandbox Code Playgroud) 我正在使用vue js开发应用程序.根据我的设置,我需要在设置更改时将变量传递给我的url.
$.get('http://172.16.1.157:8002/firstcolumn/' + c1v + '/' + c1b, function (data) {
// some code...
});
Run Code Online (Sandbox Code Playgroud)
但是当我的应用程序点击url时,它会显示以下消息.
无法加载http://172.16.1.157:8002/firstcolumn/2017-03-01/2017-10-26:从' http://172.16.1.157:8002/firstcolumn/2017-03-01/2017-重定向已被CORS政策阻止10-26 '到' http://172.16.1.157:8002 / firstcolumn://-03-1207-10-26/':没有'Access-Control-Allow-Origin'标题是出现在请求的资源上.因此,不允许来源" http:// localhost:8080 "访问.
Chrome允许我们禁用相同的原始策略,因此我们可以测试跨源请求.我想知道是否有可能在IE中做同样的事情
我使用的是Google Place API.
我希望得到关于类型帮助的地方的建议.
所以,我所做的是 -
var Google_Places_API_KEY = "AIzaSyAK08OEC-B2kSyWfSdeCzdIkVnT44bcBwM"; //Get it from - https://code.google.com/apis/console/?noredirect#project:647731786600:access
var language = "en"; //'en' for English, 'nl' for Nederland's Language
var Auto_Complete_Link = "https://maps.googleapis.com/maps/api/place/autocomplete/json?key="+Google_Places_API_KEY+"&types=geocode&language="+language+"&input=Khu";
$.getJSON(Auto_Complete_Link , function(result)
{
$.each(result, function(i, field)
{
//$("div").append(field + " ");
//alert(i + "=="+ field);
console.error(i + "=="+ field);
});
});
Run Code Online (Sandbox Code Playgroud)
所以我要求的链接是 -
如果我使用浏览器访问此链接,我可以获得类似的输出(请尝试ck) -
但是,如果我尝试使用jQuery的.getJSON或.ajax,我会通过此消息阻止我的请求 -
.
因此,XMLHTTPRequest被阻止 -
请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许原点'null'访问.
我已经在StackOverflow中检查了这个问题的解决方案并通过这里,[这里4和这里,但无法完美地得到我的解决方案.
有谁能请赐教?
我想写一个javascript函数,它返回HTML内容作为字符串给定的函数URL.我在Stackoverflow上找到了类似的答案.
我试图用这个答案来解决我的问题.
然而,似乎document.write()
没有写任何东西.当我加载页面时,我得到一个空白屏幕.
<html>
<head>
</head>
<body>
<script type="text/JavaScript">
function httpGet(theUrl)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
document.write(httpGet("https://stackoverflow.com/"));
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我已经读到了Same Origin Policy
,但是为了更好地理解这个问题:有人可以写一个简单的代码(用任何语言)来演示SOP停止的攻击吗?
在SOP出现之前怎么可能攻击某人?
这可能是重复的,但我还没有找到专门与我的问题相关的线程。
我正在进行以下 API 调用:
const config = {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,PATCH,OPTIONS"
}
};
const {
data: { ip }
} = await axios.get("https://api.ipify.org?format=json", config);
Run Code Online (Sandbox Code Playgroud)
这会引发错误:
Access to XMLHttpRequest at 'https://api.ipify.org/?format=json' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
当我将我的应用程序部署到 Heroku 时,API 调用按预期工作。但是在我的本地机器上开发时它不起作用。不确定我在这里缺少什么。
我在这里设置相当简单,如下面的代码所述.但我无法CORS
上班.我一直收到这个错误:
XMLHttpRequest无法加载http:// localhost:3000/signup.对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许来源' http:// localhost:8000 '访问.响应具有HTTP状态代码403.
我相信我在这里缺少一些简单的东西.
这是我的代码:
package main
import (
"log"
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"myApp/src/controllers"
)
func main() {
ac := new(controllers.AccountController)
router := mux.NewRouter()
router.HandleFunc("/signup", ac.SignUp).Methods("POST")
router.HandleFunc("/signin", ac.SignIn).Methods("POST")
log.Fatal(http.ListenAndServe(":3000", handlers.CORS()(router)))
}
Run Code Online (Sandbox Code Playgroud) 我的网站在Firefox和Chrome中都返回了一些奇怪的警告信息和错误.
Firefox返回以下错误:
GET http://fonts.googleapis.com/css?family=Varela+Round 200 OK 7c8e43d.js (line 18)
13:27:41.46 CSI/tbsd_
13:27:41.48 CSI/_tbnd
Run Code Online (Sandbox Code Playgroud)
Chrome会返回此错误:
XMLHttpRequest cannot load http://fonts.googleapis.com/css?family=Varela+Round. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
我猜这个问题与谷歌字体API有关,Access-Control-Allow-Origin
我该如何解决这个错误呢?