获取 CORS 请求的状态代码
Get Status Code on CORS Request
如何从 CORS 请求中return
响应代码?我能够从 onload 事件中获取它,但我似乎无法将其取出并return
以便我可以捕获它以进行值检查。
这是我基于 http://www.html5rocks.com/en/tutorials/cors/的代码:
createCORSRequest: function(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
},
makeCorsRequest: function() {
// All HTML5 Rocks properties support CORS.
var me = this;
var url = 'http://google.com';
var sCode = '';
var xhr = me.createCORSRequest('GET', url);
console.log('xhr', xhr);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
var sCode = xhr.status;
// var title = getTitle(text);
// alert('Response from CORS request to ' + url + ': ' + xhr.status);
};
xhr.onreadystatechange = function() {
console.log(this.status);
sCode = this.status;
}
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
xhr.send();
return sCode; // this returns an empty string or the initial value set to it and not from `onload` event
}
一种方法可能是在 JS 中使用回调,因为我们可以将 JavaScript 中的函数作为参数传递给其他函数,并从内部异步调用它们。
我稍微修改了您的代码以使其异步响应,并添加了相关注释。观察下面callback
的使用情况。
//make few changes to make it asynchronous.
makeCorsRequest: function(callback) {
// All HTML5 Rocks properties support CORS.
var me = this;
var url = 'http://google.com';
var sCode = '';
var xhr = me.createCORSRequest('GET', url);
console.log('xhr', xhr);
if (!xhr) {
callback('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
var sCode = xhr.status;
// var title = getTitle(text);
// alert('Response from CORS request to ' + url + ': ' + xhr.status);
callback(null, text);
};
xhr.onreadystatechange = function() {
console.log(this.status);
callback(this.status);
}
xhr.onerror = function() {
callback('Woops, there was an error making the request.');
};
xhr.send();
}
//this function would be the callback, it would be called 'Asynchronously' form XHR events with err or real data. First parameter generally indicates error.
function handleCorsResponse(err, data) {
//check if error occurred
if (err) {
//error handling here
}
//if no error occurred, proceed.
console.log(data);
}
//let's call `makeCorsRequest` with `handleCorsResponse` as a parameter.
$payments.makeCorsRequest(handleCorsResponse);
注意:一个更好和推荐的方法是使用承诺。
相关文章:
- AngularJS$http重定向状态代码
- AJAX错误状态代码500内部服务器错误
- 如何将JavaScript自动POST的状态代码获取到另一个域
- Http状态代码工厂AngularJs
- 成功ajax:如果是200状态代码,则运行函数else另一个函数
- 运行函数ifsuccess($.ajax)有200个状态代码
- XMLHttpRequest 对象状态代码为 0
- URL中包含特殊字符的HTTP状态代码
- 如何使用scalajs-angular获取请求的http状态代码
- Internet Explorer:可以't访问IFrame内容,除非状态代码为200
- 如何侦听跨域映像请求上的任何301/302重定向状态代码
- 如果 angularjs 中的状态代码为 403,请注销
- 如何让反应路由器响应 404 状态代码
- HTTP 状态代码 - Backbone.js 和 Jquery
- 如何使用 iframe.src 下载文件时获取返回状态代码
- 预检响应具有无效的 HTTP 状态代码 405 - AngularJS + Spring MVC
- 使用 ajaxError 全局处理程序显示 HTTP 状态代码 401 详细信息
- http 状态代码 NodeJS/ExpressJS 错误
- 执行服务呼叫时的状态代码 12004
- AngularJS:使用 ngResource 执行 GET 请求时的状态代码 0