PHP does not, to my knowledge, automatically JSON decode the body of the HTTP request. Here is something I turned up on Stackoverflow: http://stackoverflow.com/questions/14914628/php-decode-json-post
Basically, you cannot use the $_POST variable and will need to decode the body of the message yourself. On Wed, Nov 16, 2016 at 6:52 PM Dawg <[email protected]> wrote: > Hello everybody, > > For the past two weeks I been trying to get this simple script to work > without any success and exhausted trying to figure this out. The script > isn't displaying any error messages which makes it that much harder to > figure out what it is that I'm doing wrong. When I run the script All i get > is the square brackets [] result of this line (<pre>{{ > this.userListReceived | json }}). > > *I highlighted the problem areas in red as well as a screenshot of the > chrome console/network* > > the script itself isn't doing a lot other then retrieve a record based on > the search value userName: 'anna' > > *Side note:* > Now if i change the search value in the PHP file to: $findUser = > $_POST['userName'] = 'anna'; > I get the following results > > [{"userID": "3","userName": "anna", "password": "pass3" }] > > > any help you can provide would be greatly appreciated. > > > *Component* > @Component({ > selector: 'get-userlist', > template: `<h2>Testing</h2> > <pre>{{ this.userListReceived | json }} </pre>`, > providers: [ GetUserListService ] > }) > > export class GetUserListComponent implements OnInit { > private userListReceived: UserType[]; > private errMsg1: string; > > constructor(private getUserList: GetUserListService) {} > > getUserListButtonMethod(){ > this.getUserList.getUserListMethod() > .subscribe( > userData => this.userListReceived = userData, > error => this.errMsg1 = <any>error, > ()=>console.log('finished') > ); > } > > ngOnInit(){ > this.getUserListButtonMethod(); > } > } > > *service* > @Injectable() > > export class GetUserListService{ > private userUrl = ' > http://testing.dev/angular/app/services/getUserList.php'; > private getList: UserType[]; > > constructor( private _http: Http){} > > getUserListMethod(): Observable<UserType[]>{ > let headers = new Headers({ 'Content-Type': 'application/json' }); > let options = new RequestOptions({ headers: headers }); > > *var findU = JSON.stringify({userName: 'anna'});** // problem > area. assuming findU isn't passed through* > > return this._http > *.post(this.userUrl, findU , options) //problem area* > .map((res: Response) => res.json()) > .catch (error => <any>error); > } //END - getUserListMethod > } //END of class > > *PHP script* > <?php > require_once('dbtesting.php'); > $dbcon = Connection::getConnection(); > *$findUser = $_POST['userName'] ; //problem area, assuming $findUser isn't > receiving the value in this case userName for search* > Try{ > $STMT = $dbcon ->prepare("SELECT * FROM tbl_angular > WHERE userName = '$findUser' "); > $STMT->execute(); > $getUserlist = $STMT->fetchAll(PDO::FETCH_ASSOC); > } > catch(PDOException $e){ echo 'Unable to find user: ' . $e->getMessage(); } > echo json_encode($getUserlist); > ?> > > > > > > > > > > > > Chrome console & network output > > > <https://lh3.googleusercontent.com/-JuDS5d2bgec/WCztuXwoTMI/AAAAAAAAA98/wsEmzOzCY345ydRTgY3WhAN0kPa0NscewCLcB/s1600/prj.PNG> > > > -- > You received this message because you are subscribed to the Google Groups > "Angular" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/angular. > For more options, visit https://groups.google.com/d/optout. > -- Lucas Lacroix Computer Scientist Advanced Technology Division, MEDITECH <http://ehr.meditech.com/> 781-774-2293 -- You received this message because you are subscribed to the Google Groups "Angular" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
