所以我理解并知道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); }
$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 !") ); }
$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);