我无法从 JSONP 对 Angular2 的响应中获取正文数据,但它可以通过使用 ajax
I cannot get the body data from JSONP's response with Angular2, but it works by using ajax
首先,我使用 spring-boot 对 SERVER 进行编码,代码如下:
public class App {
@RequestMapping("/")
@ResponseBody
String home(HttpServletRequest request) {
String aa=request.getParameter("callback");
System.out.println(request.getParameter("callback"));
return aa+"({\"text\":\"hello,world\"})";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}}
其次,我用 Angular2 编写了前端:
export class HomePage implements OnInit{
constructor(private jsonp:Jsonp) {
}
ngOnInit(): void {
this.jsonp.get("http://localhost:8080/callback=JSONP_CALLBACK")
.subscribe(res=>console.log(this.res);
);
}}
然后,我 运行 在 ng-serve 中的前端,并从控制台获取信息:
Response {_body: Object, status: 200, ok: true, statusText: "Ok", headers: Headers…}
显然,“_body: Object”中有我想要的数据,实际上,数据如下所示:
{"text":"hello,world"}
所以我试图获取数据,但是 Observable 的运算符 "subscribe" 只有这些方法:
click here
所以我选择了 json 方法,但我从控制台得到了这些:
function () {
if (typeof this._body === 'string') {
return JSON.parse(/** @type {?} */ (this._body));
}
if (this._body instanceof ArrayBuffer) {
return …
所有的方法我都试过了,就是获取不到body数据,除了这个奇怪的方法:
console.log(res._body.text);
当然有编译错误,但是我确实得到了数据:click here
我用AJAX试了一下上面的问题都没有出现,我可以很容易的得到这样的数据
jQuery(document).ready(function(){
$.ajax({
type: "get",
async: false,
url: "http://localhost:8080/",
dataType: "jsonp",
jsonp: "callback",
jsonpCallback:"JSONP_CALLBACK",
success: function(json){
console.log(json.text);
},
error: function(){
alert('fail');
}
});
});
那么,我如何在 Angular2 中使用 rxjs 的 Observable 获取 json 数据,或者这是一个错误?
您应该能够将响应映射为 JSON:
this.jsonp.get("http://localhost:8080/callback=JSONP_CALLBACK")
.map(res => res.json())
.subscribe(data => console.log(data));
首先,我使用 spring-boot 对 SERVER 进行编码,代码如下:
public class App {
@RequestMapping("/")
@ResponseBody
String home(HttpServletRequest request) {
String aa=request.getParameter("callback");
System.out.println(request.getParameter("callback"));
return aa+"({\"text\":\"hello,world\"})";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}}
其次,我用 Angular2 编写了前端:
export class HomePage implements OnInit{
constructor(private jsonp:Jsonp) {
}
ngOnInit(): void {
this.jsonp.get("http://localhost:8080/callback=JSONP_CALLBACK")
.subscribe(res=>console.log(this.res);
);
}}
然后,我 运行 在 ng-serve 中的前端,并从控制台获取信息:
Response {_body: Object, status: 200, ok: true, statusText: "Ok", headers: Headers…}
显然,“_body: Object”中有我想要的数据,实际上,数据如下所示:
{"text":"hello,world"}
所以我试图获取数据,但是 Observable 的运算符 "subscribe" 只有这些方法: click here
所以我选择了 json 方法,但我从控制台得到了这些:
function () {
if (typeof this._body === 'string') {
return JSON.parse(/** @type {?} */ (this._body));
}
if (this._body instanceof ArrayBuffer) {
return …
所有的方法我都试过了,就是获取不到body数据,除了这个奇怪的方法:
console.log(res._body.text);
当然有编译错误,但是我确实得到了数据:click here
我用AJAX试了一下上面的问题都没有出现,我可以很容易的得到这样的数据
jQuery(document).ready(function(){
$.ajax({
type: "get",
async: false,
url: "http://localhost:8080/",
dataType: "jsonp",
jsonp: "callback",
jsonpCallback:"JSONP_CALLBACK",
success: function(json){
console.log(json.text);
},
error: function(){
alert('fail');
}
});
});
那么,我如何在 Angular2 中使用 rxjs 的 Observable 获取 json 数据,或者这是一个错误?
您应该能够将响应映射为 JSON:
this.jsonp.get("http://localhost:8080/callback=JSONP_CALLBACK")
.map(res => res.json())
.subscribe(data => console.log(data));