如何连接Angular 2和php后端(mysql)
发布时间:2020-12-17 17:32:09 所属栏目:安全 来源:网络整理
导读:所以我理解并知道ajax是如何工作的,因为我之前已经使用过它.但是如何从php脚本中检索 JSON数据呢? 我试图回应json_encode结果(这是有效的),但我得到一个例外:未捕获(在承诺中):意外的令牌在Chrome中的位置0错误的JSON中. 错误的行: search(): any { this
所以我理解并知道ajax是如何工作的,因为我之前已经使用过它.但是如何从php脚本中检索
JSON数据呢?
我试图回应json_encode结果(这是有效的),但我得到一个例外:未捕获(在承诺中):意外的令牌<在Chrome中的位置0错误的JSON中. 错误的行: search(): any { this.http.get('app/php/search.php').toPromise() .then(response => this.data = response.json()) .catch(this.handleError); } Php文件(用于测试目的): $json = array ( "age" => 5,"bob" => "Lee",); echo json_encode($json); 解决方法
你为什么不使用observables ..我在我的虚拟项目中使用了php作为后端.这是它的代码.
ngOnInit() { let body=Path+'single.php'+'?id=' + this.productid; console.log(body); this._postservice.postregister(body) .subscribe( data => { this.outputs=data; // console.log(this.outputs); },error => console.log("Error HTTP Post Service"),() => console.log("Job Done Post !") ); } PHP代码 $faillogin=array("error"=>1,"data"=>"no data found"); $successreturn[]=array( "productid"=>"any","productname"=>"any","productprice"=>"any","productdescription"=>"any","productprimaryimg"=>"any","otherimage"=>"any","rating"=>"any"); // Create connection id,name,price,description,primary_image,other_image,rating $productid = $_GET["id"]; $sql="SELECT * FROM product_list where id='$productid'"; $result = mysqli_query($conn,$sql); $count = mysqli_num_rows($result); $value=0; while($line = mysqli_fetch_assoc($result)) { $successreturn[$value]['productid']=$line['id']; $successreturn[$value]['productname']=$line['name']; $successreturn[$value]['productprice']=$line['price']; $successreturn[$value]['productdescription']=$line['description']; $successreturn[$value]['productprimaryimg']=$line['primary_image']; $successreturn[$value]['otherimage']=$line['other_image']; $successreturn[$value]['rating']=$line['rating']; $value++; } echo json_encode($successreturn); mysqli_close($conn); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读