Angular2 不发送CORS HTTPAJAX请求
Angular 2 does not send CORS HTTP AJAX request
Angular 2.0.0-rc6
我正在尝试通过 CORS AJAX 调用访问 CouchDB 服务器,如下所示。
let headers = new Headers();
headers.append('Authorization', 'Basic YWRtaW46TjI4Q29uMy4xYjItNDE=');
let options = new RequestOptions({ headers : headers, withCredentials: true });
this.http.get(this.couchDbUrl, options)
.map(this.extractData)
.catch(this.handleError);
console.log(this.couchDbUrl);
Web 浏览器没有发出 OPTIONS
或 GET
请求。 catch
& map
回调不会被触发。也完全没有错误!
如果我用 jQuery 替换代码,效果会很好。
jQuery.ajax({
method : 'GET',
url : this.couchDbUrl,
cache : false,
crossDomain : true,
xhrFields: {
withCredentials: true
},
headers : { 'Authorization' : 'Basic YWRtaW46TjI4Q29uMy4xYjItNDE=' }
});
我不想涉及 jQuery,Angular 2 HTTP 客户端有什么问题?
您将需要subscribe
。
this.http.get(this.couchDbUrl, options)
.map(this.extractData)
.subscribe( data => ...) // Subscribe
.catch(this.handleError);
更多信息:https://angular.io/docs/ts/latest/guide/server-communication.html
Angular 2.0.0-rc6
我正在尝试通过 CORS AJAX 调用访问 CouchDB 服务器,如下所示。
let headers = new Headers();
headers.append('Authorization', 'Basic YWRtaW46TjI4Q29uMy4xYjItNDE=');
let options = new RequestOptions({ headers : headers, withCredentials: true });
this.http.get(this.couchDbUrl, options)
.map(this.extractData)
.catch(this.handleError);
console.log(this.couchDbUrl);
Web 浏览器没有发出 OPTIONS
或 GET
请求。 catch
& map
回调不会被触发。也完全没有错误!
如果我用 jQuery 替换代码,效果会很好。
jQuery.ajax({
method : 'GET',
url : this.couchDbUrl,
cache : false,
crossDomain : true,
xhrFields: {
withCredentials: true
},
headers : { 'Authorization' : 'Basic YWRtaW46TjI4Q29uMy4xYjItNDE=' }
});
我不想涉及 jQuery,Angular 2 HTTP 客户端有什么问题?
您将需要subscribe
。
this.http.get(this.couchDbUrl, options)
.map(this.extractData)
.subscribe( data => ...) // Subscribe
.catch(this.handleError);
更多信息:https://angular.io/docs/ts/latest/guide/server-communication.html