需要帮助,角贴,PHP mysql [英] Help needed , angular post , PHP mysql
本文介绍了需要帮助,角贴,PHP mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用angular 4,php mysql。
我从angular发送一个帖子给php mysql。
我想得到你在下面的代码中看到的php json的响应,到角度,知道数据是否已经提交。
My Angular Script
从'@ angular / core'导入{Injectable};来自'@ angular / http'的
import {Http,Response,RequestOptions,Headers};来自'rxjs / Observable'的
import {Observable};
import'rxjs / add / operator / map'
@Injectable()
export class appService {
postResponse:any;
状态;
构造函数(私有http:Http){}
insertData(){
let headers = new Headers({'Content-Type':'application / json'});
let options = new RequestOptions({headers:headers});
返回this.http.post('http:// localhost:80 / angularsql / sql.php',JSON.stringify({firstName:'Joe',lastName:'Smith333'}),{headers:headers })
.map((res:Response)=> res.json())
.subscribe((res:'')=> this.postResponse = res);
}
}
我的php脚本:
<?php
header(Access-Control-Allow-Origin:* );
header(Access-Control-Allow-Methods:PUT,GET,POST);
header(Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept);
$ data = json_decode(file_get_contents('php:// input'),true);
$ fname = $ data ['firstName'];
$ lname = $ data ['lastName'];
$ con = new mysqli('localhost','root','','angular');
if($ fname =='anan'&& $ lname =='kassis'){
$ sql =insert into users_tbl(firstName,lastName)values('。$ fname。 '' $ L-NAME ')。
$ result = $ con> query($ sql);
$ data = [status=> 详细插入];
echo json_encode($ data);
}
else {
$ data = [status=> 没有插入细节];
echo json_encode($ data);
}
?>
我尝试了什么:
i已经尝试了一切,我无法将json从php恢复到angular
解决方案
数据= json_decode(file_get_contents('php:// input'),true);
fname =
data ['firstName'];
I am using angular 4 , php mysql. i am sending a post to php mysql from angular. i want to get the response of the php json as you see in the code below , to the angular , to know , whether the data has been submitted or not . My Angular Script import { Injectable } from '@angular/core'; import { Http, Response ,RequestOptions,Headers} from '@angular/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map' @Injectable() export class appService { postResponse:any ; status; constructor(private http:Http) { } insertData() { let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); return this.http.post('http://localhost:80/angularsql/sql.php',JSON.stringify({firstName:'Joe',lastName:'Smith333'}),{headers:headers}) .map((res: Response) => res.json()) .subscribe((res:'') => this.postResponse = res); } } My php Script : <?php header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: PUT, GET, POST"); header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); $data = json_decode( file_get_contents('php://input'),true ); $fname = $data['firstName']; $lname = $data['lastName']; $con = new mysqli('localhost','root','','angular'); if($fname=='anan' && $lname=='kassis') { $sql = "insert into users_tbl(firstName,lastName) values('".$fname."','".$lname."')"; $result = $con->query($sql); $data = [ "status" => "details inserted" ]; echo json_encode($data); } else { $data = [ "status" => "didn't insert details" ]; echo json_encode($data); } ?>
What I have tried:
i have tried everything , i am not able to get back the json from the php to the angular
解决方案
data = json_decode( file_get_contents('php://input'),true );
fname =
data['firstName'];
这篇关于需要帮助,角贴,PHP mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文