我已经搜索了很多关于这个问题的解决方案,大多数是关于PHP的,但都不起作用。我正在尝试向j2ee servlet发送POST参数,但是servlet没有接收到参数。虽然如果我将参数与URL(GET)一起发送,则能够接收数据。
以下是代码:
这是Servlet代码:
注意:我是 Ionic 2 的新手。
以下是代码:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/Rx';
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
userName: string;
password: string;
constructor(public navCtrl: NavController, public navParams: NavParams,
public http: Http) {
}
login() {
var headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let devicecode = '1-001-001-009-1';
let postParams = {
loginid: this.userName,
password: this.password,
devicecode: devicecode
}
this.http.post("http://localhost:8092/OCWebService/LoginOSO", postParams
, options)
.subscribe(data => {
console.log(data);
alert(data);
}, error => {
console.log(error);// Error getting the data
});
}
}
这是Servlet代码:
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
response.setContentType("text/html;charset=UTF-8");
BufferedReader bf = request.getReader();
String userName = request.getParameter("loginid");
String password = request.getParameter("password");
String deviceCode = request.getParameter("devicecode");
}
注意:我是 Ionic 2 的新手。