如何使用Http.post(Angular 2)发布JSON对象(PHP服务器端)

如何使用Http.post(Angular 2)发布JSON对象(PHP服务器端),第1张

如何使用Http.post(Angular 2)发布JSON对象(PHP服务器端

角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-urlenpred'});  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文件:

  <?phpheader('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 connectionif (!$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_enpre($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_enpre($accountCreated);        } ?>

我希望这将为您工作..对于json,我想您应该将其作为选项使用,并使用json解码获得在选项中获得的值。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5674118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存