angular - ionic2 not sending post data -


following function in auth.service.ts calling 1 of pages..

import { injectable } '@angular/core'; import { http, headers } '@angular/http'; import { storage } '@ionic/storage'; import 'rxjs/add/operator/map';  @injectable() export class authservice {    public token: any;    constructor(public http: http, public storage: storage) {    } login(credentials){  return new promise((resolve, reject) => {     console.log(credentials);     let headers = new headers();     headers.append('content-type', 'application/json');     this.http.post('https://mysite.co.uk/ionlogin.php', json.stringify(credentials), {headers: headers})       .subscribe(res => {          let data = res.json();         this.token = data.token;         this.storage.set('token', data.token);         resolve(data);          resolve(res.json());       }, (err) => {         reject(err);       });  });  } } 

the above login function called doesn't send data. in console console.log(credentials) prints before post request.

{email: "sugumar", password: "123456"}

but in network tab in browser, don't see data sent server.

and in the server printing using

var_dump(json_decode(file_get_contents("php://input")));

it prints null in network tab in browser

i see following error in console

xmlhttprequest cannot load https://mysite.co.uk/ionlogin.php. request header field access-control-allow-origin not allowed access-control-allow-headers in preflight response.

so added following lines in php

header("access-control-allow-origin: *"); header("access-control-allow-credentials: true "); header("access-control-allow-methods: options, get, post"); header("access-control-allow-headers: content-type, depth, user-agent, x-file-size,      x-requested-with, if-modified-since, x-file-name, cache-control"); 

but still same problem

try using requestoptions instead of sending header object directly

import { http, headers, requestoptions } '@angular/http';  public login = () => {   return new promise((resolve, reject) => {     let headers = new headers({'content-type', 'application/json'});     // took off append cause way, there's no need remove it.     let options = new requestoptions({headers: headers});     this.http.post('https://mysite.co.uk/ionlogin.php', json.stringify(credentials), options)     .subscribe(res => {        let data = res.json();       this.token = data.token;       this.storage.set('token', data.token);        // why resolve twice? it'll first resolve       resolve(data);       resolve(res.json());     }, (err) => {       reject(err);     });   }); } 

the requestoptions module create options based on fetch spec requisitions, maybe passing headers object making fail or missing provided request option.

hope helps.


Comments

Popular posts from this blog

php - Cannot override Laravel Spark authentication with own implementation -

Qt QGraphicsScene is not accessable from QGraphicsView (on Qt 5.6.1) -

What is happening when Matlab is starting a "parallel pool"? -