如何在2角一个简单的JSONP异步请求? [英] How to make a simple JSONP asynchronous request in Angular 2?
问题描述
我想下面的角1 code转变为角2:
I'm trying to convert the following Angular 1 code to Angular 2:
$http.jsonp('https://accounts.google.com/logout');
它需要一个JSONP请求跳过CORS的政策问题。
It needs to be a JSONP request to skip the CORS policy issue.
推荐答案
如果这个端点是符合JSONP,可以使用以下。你需要找出参数用来提供JSONP回调。在下面的code,我称之为 C
。
If this endpoint is jsonp-compliant, you can use the following. You need to find out the parameter to use to provide the jsonp callback. In the code below, I call it c
.
已经注册时调用 JSONP_PROVIDERS
的引导
之后的功能:
After having registered JSONP_PROVIDERS
when calling the bootstrap
function:
import {bootstrap} from 'angular2/platform/browser'
import {JSONP_PROVIDERS} from 'angular2/http'
import {AppComponent} from './app.component'
bootstrap(AppComponent, [ JSONP_PROVIDERS ]);
您可以然后使用您的构造函数注入 JSONP
类的一个实例执行您的要求:
You can then execute your request using an instance of the Jsonp
class you injected from constructor:
import {Component} from 'angular2/core';
import {Jsonp} from 'angular2/http';
@Component({
selector: 'my-app',
template: `
<div>
Result: {{result | json}}
</div>
`
})
export class AppComponent {
constructor(jsonp:Jsonp) {
var url = 'https://accounts.google.com/logout&c=JSONP_CALLBACK';
jsonp.request(url, { method: 'Get' })
.subscribe((res) => {
(...)
});
}
}
有关详细信息,请参阅此问题:
See this question for more details:
- I need to do a http request to a mail chimp subscription list via a component post
这篇关于如何在2角一个简单的JSONP异步请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!