我有以下问题:
我正在开发一个Ionic 2项目(在http://localhost:8100/上测试)
此外,我已经在Java中建立了一个Web服务,可以在http://localhost:8080/pde_webservice/test下访问。经过我的浏览器测试,它的工作原理。
但是我无法向上面的URI发送帖子请求..在err
中我得到了URL为空的奇怪信息..
{"body":{"isTrusted":true}, "status":0,"ok":false,
"statusText":"","headers":{},"type":3,"url":null}
这里是获取URI的来源。
public login(username, password) {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
// headers.append('Content-Type', 'text/html; charset=UTF-8');
var postData = "user=" + username + "&password=" + password;
var sub = this.http.post("http://localhost:8080/pde_webservice/test", postData , {headers})
.map(res => res.json());
sub.subscribe(
data => {
// this.storage.set('token', data.token);
// console.log("Token: " + this.storage.get("token"));
},
err => {
console.log(JSON.stringify(err));
});
return sub;
}
也尝试使用HTTP GET
,但使用相同的ERR
消息
public login(username, password) {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
// headers.append('Content-Type', 'text/html; charset=UTF-8');
var postData = "user=" + username + "&password=" + password;
let params: URLSearchParams = new URLSearchParams();
params.set('username', username);
params.set('password', password);
var sub = this.http.get("http://localhost:8080/pde_webservice/test", {search : params})
.map(res => res.json());
sub.subscribe(
data => {
// this.storage.set('token', data.token);
// console.log("Token: " + this.storage.get("token"));
},
err => {
console.log("test");
console.log(JSON.stringify(err));
});
return sub;
}
答案 0 :(得分:2)
从中得到一个想法:
myfunc(username, password){
//access token info
let status;
let url = 'http://188.166.227.128:8080/oauth/token';
let grant_type = 'password';
let scope = 'clientappscope';
let client_id = '1';
let client_secret = 'VZlmB1DbcmJiY0BWvOzfkSo2KGHoV0gURzYuJd2T';
username = 'appuser@fidelito.com';
password = 'password';
let body =
'grant_type=' + grant_type +
'&scope=' + scope +
'&client_id='+ client_id +
'&client_secret='+ client_secret +
'&username='+ username +
'&password='+ password;
let headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let loader = this.loadingCtrl.create({
content: "Please wait...",
spinner: 'crescent'
});
loader.present();
this.http.post(url, body, options)
.map(res => res.json())
.subscribe(data => {
});
基本上将标题传递给RequestOptions
对象,以及您想要通过正文的参数。