0
return
CORSリクエストのレスポンスコードはどうですか?私はonloadイベントからそれを得ることができますが、私はそれを取り出して、return
を作ることができないので、私は値のチェックのためにそれを捕まえることができます。ここでCORSリクエストのステータスコードを取得
はhttp://www.html5rocks.com/en/tutorials/cors/に基づいて私のコードです:私たちは他の関数へのパラメータとしてJavaScriptで関数を渡すと、内部から非同期的にそれらを呼び出すことができるよう
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
}
レスポンスが返される前に「ステータス」を返すように見えます。 'onerror'、' onload'などのイベントは非同期的に起動され、 'status'コードしか利用できないことを考慮する必要があります。コールバックやプロミスが役に立ちます。 – Ankit
'makeCorsRequest'をどのように呼び出しているのですか? – Ankit
こんにちは@AnkitMishra。私はAngularJSを使用していますので、私は工場からそれを 'statCode = $ payments.makeCorsRequest();' – ralphcarlo