我正在尝试从角度2)重新创建PostJSON到php但由于$_REQUESTphp端的变量中没有任何内容,因此无法正常工作
$_REQUEST
代码:
searchHttp({body}: any): Promise<any> { let headers = new Headers ({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers, method: "post" }); let test_this = {"search": "person"}; return this.http.post(this.post_url, JSON.stringify(test_this), options) .toPromise() .then(response => { return response.text(); }) .catch(this.handleError); }
有什么我想念的吗?我知道帖子可以使用其他格式,因为我已经在另一个问题中回答了该问题。
还有,http.request胜过http.post?
http.request
http.post
编辑:
经过与Angular / Javascript专家的大量协商,他们认为这是一个php问题。因此,任何了解如何在php端接受JSON对象的人都将受到欢迎。
角2客户端部分
ngOnInit() { let body=Api+'product.php'+'?id=' + this.link_id; this._callservice.callregister(body) .subscribe( data => { this.outputs=data; }, error => console.log("Error HTTP Post"), () => console.log("completed") ); } }
呼叫服务
import {Injectable} from '@angular/core'; import {Router} from '@angular/router'; import {Http, Response, Headers, RequestOptions} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import 'rxjs/add/observable/throw'; @Injectable() export class AuthenticationService { constructor(private _http:Http){} postregister(api:any){ // console.log(api); let headers = new Headers({'Content-Type':'application/x-www-form-urlencoded'}); let options = new RequestOptions({ headers: headers, method: "post"}); return this._http.get(api,options) .map(res => res.json()) .catch(this.handleError); } private handleError (error: Response) { console.error(error); return Observable.throw(error.json().error || ' error'); } }
服务器端PHP确保在服务器端PHP代码中包含以下三行。
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: X-Requested-With'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
php文件:
<?php header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: X-Requested-With'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); $servername = "localhost"; $username1 = "root"; $password = "root"; $dbname = "product"; $e=array("error"=>1,"message"=>"Account Already Exists"); $accountCreated = array( "error" =>0, "data" => array( "username" => "amit" , "password" => "anypassword", "role"=> "user", "id" => "anyid" ) ); // Create connection $conn = mysqli_connect($servername, $username1, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $username = $_GET["username"]; $Pass = $_GET["password"]; $role= $_GET["role"]; $sql="SELECT COUNT(*) as user FROM users WHERE username = '$username'"; $result = mysqli_query($conn,$sql); $line = mysqli_fetch_assoc($result); $count = $line['user']; if($count!=0) { echo json_encode($e); } else { $sql="INSERT INTO users(username,password,role)VALUES('$username','$Pass','$role')"; $result=mysqli_query($conn,$sql); $sql="select * from users where username ='$username'"; $result=mysqli_query($conn,$sql); $line=mysqli_fetch_assoc($result); { $accountCreated['data']['username']=$line['username']; $accountCreated['data']['password']=$line['password']; $accountCreated['data']['role']=$line['role']; $accountCreated['data']['id']=$line['id']; } echo json_encode($accountCreated); } ?>
我希望这将为您工作..对于json,我想您应该将其作为选项使用,并使用json解码获得在选项中获得的值。