Matej
Matej

Reputation: 147

HTTP Post with Ionic 2

I have the following problem:

Im developing currently a Ionic 2 Project (testing on http://localhost:8100/) Furthmore I have set up a webservice in Java, reachable under http://localhost:8080/pde_webservice/test. Tested with my browser, it works. But I'm not able to send a post request to the URI above.. in err I get the weird information that the URL is null..

{"body":{"isTrusted":true}, "status":0,"ok":false,
"statusText":"","headers":{},"type":3,"url":null}

Here the source to grab the 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;
     }

Tried also with HTTP GET, but with same ERR message

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;

} 

Upvotes: 1

Views: 1499

Answers (1)

Kumal Pereira
Kumal Pereira

Reputation: 69

Get an idea from this:

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 = '[email protected]';
  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 => {

});

Basically pass your headers through a RequestOptions object, and the parameters you want through the body.

Upvotes: 2

Related Questions