I've exhausted my search on Google and tried numerous PHP workarounds to no avail. My issue lies in attempting to send POST parameters to a j2ee servlet, as the parameters are not being received at the servlet. Strangely though, I can successfully receive data when sending parameters via URL attachment (GET).
Below is the Angular 4 code snippet:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Http, Headers, RequestOptions } from '@http/client';
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
});
}
}
And here's the corresponding Servlet Code:
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");
}
Please note that I'm relatively new to working with Ionic 2.