4

I am beginner in the Angular CLI, I have used the login api:'http://localhost/appointjobs/index.php/admin_api/index' using http.post but, I didn't get the post data in server side(codeigniter/php) when set 'content-type:application/json'. Below code I have used in the login services, and also getting post data when I used 'application/x-www-form-urlencoded' instead of 'application/json'.

DataService.ts file:  

import { BadInputError } from './../common/bad-input-error';
import { error } from 'selenium-webdriver';
import { AppError } from './../common/app-error';
import { Observable } from 'rxjs/Observable';
import { Http, ResponseOptionsArgs,RequestOptionsArgs,Headers,RequestOptions } from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import { NotFoundError } from '../common/not-found-error';
import { Response } from '@angular/http/src/static_response';
import { HttpHeaders } from '@angular/common/http';



 @Injectable()
 export class DataService {

    constructor(private http:Http) {
 }



   getWhere(url,resource){
     let headers= new Headers();
     //headers.append('Access-Control-Allow-Origin:','*');
     headers.append('Accept','text/plain');
     headers.append('content-type','application/json');
     //headers.append('content-type','application/x-www-form-urlencoded');
     let option= new RequestOptions({headers:headers});

    return this.http.post(url,JSON.stringify(resource),option)
      .map(response=>response.json())
    .catch(this.handleError);
   }
 }

AuthService.ts file:

import { DataService } from './data.service';
import { Injectable } from '@angular/core';



@Injectable()
export class AuthService{

private url = 'http://localhost/appointjobs/index.php/admin_api/index';
constructor(private dataService:DataService) {
}

signIn(params:HTMLInputElement){
  this.dataService.getWhere(this.url,params)
  .subscribe(response=>{
    console.log(response);
  });
 }
}
14
  • do you do this.getWhere(url, resource).subscribe() anywhere? without a subscribe the call never actually fires Commented Jan 31, 2018 at 10:05
  • You don't have to specify headers.append('content-type','application/json'); Commented Jan 31, 2018 at 10:08
  • 1
    1. Don't use Http. It's deprecated. Use HttpClient, as documented: angular.io/guide/http. 2. If your backend API expects JSON, you need to send JSON. If it expects application/x-www-form-urlencoded, you need to set the right content type header, and to pass key/value pairs, in the application/x-www-form-urlencoded format. Setting the content type to application/x-www-form-urlencoded and sending a JSON body doesn't make any sense. Commented Jan 31, 2018 at 10:16
  • @JBNizet depends if he is using angular2/4 or angular 5 but yes, HttpClient is the better option Commented Jan 31, 2018 at 10:16
  • @mast3rd3mon he's using angular 4. But even if he was using angular 2, the first step should be to upgrade. Commented Jan 31, 2018 at 10:19

1 Answer 1

2

Use FormData send your data to php

change your service to below

getWhere(url,resource){

     const formData: FormData = new FormData();
     formData.append('data', JSON.stringify(resource));

     let headers= new Headers();
     headers.append('Accept', 'application/json');

    return this.http.post(url,formData, { headers: headers })
      .map(response=>response.json())
    .catch(this.handleError);
   }
 }

and in your php

print_r($_POST['data']); // gives you the json

use

json_decode($_POST['data']) // converts your json string into object
Sign up to request clarification or add additional context in comments.

17 Comments

I try with fromData but getting blank into post data.
is your resource contains the data check that
Yes getting data in post but incorrect format check this post data {"data":"{\"username\":\"ffdsfdsfdsfdsf\",\"password\":\"dsfdsfdsfsfsfs\"}"}
your getting this data in php ?
you have to decode the json to get desired result try json_decode($_POST['data])
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.