我正在提出CORS xhr请求.这在chrome中运行良好,但是当我在safari中运行时,我得到一个'无法加载---- Access-control-allow-origin不允许访问'.代码完全相同,我在服务器上设置了CORS.下面是我的代码.(有访问控制,但你可以免费试用没有accessToken)
var water;
var req = new XMLHttpRequest;
req.overrideMimeType("application/json");
req.open('GET', 'https://storage.googleapis.com/fflog/135172watersupplies_json', true);
req.setRequestHeader('Authorization', 'Bearer ' + accessToken);
origThis = this;
var target = this;
req.onload = function() {
water = req;
req.send(null);
Run Code Online (Sandbox Code Playgroud)
查看请求标头后,我看到首先发出OPTIONS请求,这是不允许的请求.原始标头不包含在Safari中的响应中,但是在chrome中.什么会导致这种情况.任何帮助将不胜感激.
更新:我已经尝试在Safari for Windows中运行,所以我不确定这里发生了什么.我使用的mac是远程访问(Macincloud.com),但我不认为这与它有任何关系.
我正在尝试实现简单的xhr抽象,并在尝试设置POST的标头时收到此警告.我认为它可能与在单独的js文件中设置标题有关,因为当我<script>在.html文件中的标记中设置它时,它工作正常.POST请求工作正常,但我收到此警告,我很好奇为什么.我得到这样的警告两个content-length和connection头,但仅在WebKit的浏览器(Chrome浏览器5测试版和Safari 4).在Firefox中,我没有收到任何警告,Content-Length标头设置为正确的值,但Connection设置为keep-alive而不是close,这让我觉得它也忽略了我的setRequestHeader调用并生成它自己的.我没有在IE中试过这段代码.这是标记和代码:
test.html:
<!DOCTYPE html>
<html>
<head>
<script src="jsfile.js"></script>
<script>
var request = new Xhr('POST', 'script.php', true, 'data=somedata', function(data) {
console.log(data.text);
});
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
jsfile.js:
function Xhr(method, url, async, data, callback) {
var x;
if(window.XMLHttpRequest) {
x = new XMLHttpRequest();
x.open(method, url, async);
x.onreadystatechange = function() {
if(x.readyState === 4) {
if(x.status === 200) {
var data = {
text: x.responseText,
xml: x.responseXML
};
callback.call(this, data);
}
}
} …Run Code Online (Sandbox Code Playgroud) 我正在使用谷歌日历玩萤火虫.我碰巧发现某些XHR请求有如下响应:
while(1);[['us','bW9yZ2FuLmNoZW5nbW9AZ21haWwuY29t 20090320/20090904 63378122163']]
Run Code Online (Sandbox Code Playgroud)
它看起来像带有前缀死循环javascript语句的JSON.
我不确定为什么Google Calendar会有这种XHR响应.有关于此的任何已知的AJAX实践吗?
var xhttp=new XMLHttpRequest();
xhttp.open('GET', 'foo.xml', false);
Run Code Online (Sandbox Code Playgroud)
F12弹出:SCRIPT5:访问被拒绝.在第95行,这是xhttp.open行.
我的JavaScript似乎格式正确,Firefox做了我认为应该做的事情.
我已经阅读了很多与此非常类似的问题,所以我查看了同源策略,但我看不出它是如何应用的,因为foo.xml与html文件位于同一目录中.我在我的本地Intranet上打开了脚本权限,并告诉迈克菲休息五分钟,这是肯定的.我甚至试过以管理员身份运行IE,所以这不是真正的权限问题吗?为什么IE会被拒绝访问本地文件?
我正在使用angular 1.1.5并且我使用$ resource为一个REST服务创建一个XHR,但似乎$资源没有将标头附加为X-Requested-With作为XMLHttpRequest,是一个正常的行为?我需要手动附加标题吗?
function loginCtrl($scope,$resource) {
$scope.submit = function () {
var resource = $resource('/Api/User/login', {},
{
authenticate: {
method: 'POST',
isArray: false,
headers: {
'__RequestVerificationToken': $scope.loginRequest.Token
}
}
});
resource.authenticate($scope.loginRequest);
};
}
Run Code Online (Sandbox Code Playgroud) 我正在写我的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的问题...我已经尝试了几个解决方案,但我没有解决问题.
我该如何解决这个问题?
我必须添加什么代码来修复它?
看起来根据 CORS规范,GET和POST请求应该透明地遵循302重定向.但Chrome正在取消我的请求.
这是执行请求的JS:
var r = new XMLHttpRequest();
r.open('GET', 'https://dev.mysite.com/rest', true);
r.send();
Run Code Online (Sandbox Code Playgroud)
这是应该发生的事情:
但在第2步之后,Chrome会取消该请求.如果没有HTTP 302,请求将完美地运行.我已经证实了这一点.
当请求运行时,我可以在Chrome的"网络"面板中看到只有一个XHR - 取消的POST请求,没有响应标头或响应正文.
使用Chrome的net-internals工具进行调试,我看到服务器发送了响应,之后,请求被取消了.以下是请求的输出:
79295: URL_REQUEST
https://dev.mysite.com/rest
Start Time: 2013-08-30 12:41:11.637
t=1377880871637 [st= 0] +REQUEST_ALIVE [dt=13455]
t=1377880871638 [st= 1] URL_REQUEST_BLOCKED_ON_DELEGATE [dt=1]
--> delegate = "extension Adblock Plus"
t=1377880871639 [st= 2] +URL_REQUEST_START_JOB [dt=13453]
--> load_flags = 143540480 (DO_NOT_SAVE_COOKIES | DO_NOT_SEND_AUTH_DATA | DO_NOT_SEND_COOKIES | ENABLE_LOAD_TIMING | MAYBE_USER_GESTURE | REPORT_RAW_HEADERS | VERIFY_EV_CERT)
--> method = "POST"
--> priority = 2 …Run Code Online (Sandbox Code Playgroud) 我正在使用Rails 4创建一组服务,我正在使用JavaScript浏览器应用程序.跨源GETS工作正常,但我的POST未通过预检OPTIONS检查404错误.至少,我认为这是正在发生的事情.以下是控制台中出现的错误.这是Mac上的Chrome 31.0.1650.63.
OPTIONS http://localhost:3000/confessor_requests 404 (Not Found) jquery-1.10.2.js:8706
OPTIONS http://localhost:3000/confessor_requests No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. jquery-1.10.2.js:8706
XMLHttpRequest cannot load http://localhost:3000/confessor_requests. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. main.html:1
Run Code Online (Sandbox Code Playgroud)
我已经搜索了关于启用CORS的指令的高低,我很难过.通常的建议似乎是将这样的东西放在Application控制器中,我做了.
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = '*'
headers['Access-Control-Max-Age'] = "1728000"
end
def cors_preflight_check
if request.method == :options
headers['Access-Control-Allow-Origin'] = …Run Code Online (Sandbox Code Playgroud) xmlhttprequest rails-routing http-method cors ruby-on-rails-4
理解XMLHttpRequest处理程序时遇到一些问题.规范说明了这个onerror处理程序:
error[Dispatched ...]请求失败时.
load[Dispatched ...]请求成功完成后.
问题是,"请求失败了"是什么意思.那可能是
此外,我想知道它是否意味着onerror并且onload永远不应该同时开火.
此引用表示onerror应根据status代码执行处理程序并onload依赖于readyState.这表明它们并不相互排斥,但是,我不认为这是一个权威的信息.
我问,因为使用最新的Opera快照,我发现onload甚至在404状态代码上被解雇.我知道测试status是一个肯定的赌注,但我想知道这是我必须按照规范做什么,或者只是解决Opera中的错误.
xmlhttprequest ×10
javascript ×7
cors ×4
ajax ×3
angularjs ×2
html ×1
http ×1
http-method ×1
json ×1
redirect ×1
safari ×1