TransWikia.com

Как получить JSON в PHP через POST

Stack Overflow на русском Asked by LAMER on November 24, 2021

Есть код на Angular который направляет JSON:

    import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class HttpSendDataService {


  constructor(private http: HttpClient){ }

  httpsend1(data: any)
  {
      console.log(data);
      return this.http.post('http://localhost/server/addDrugCrime.php/', this.data);
  }
}

И код на PHP для получения и обработки:

<?php
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Expose-Headers", "Access-Control-*");
    header("Access-Control-Allow-Headers", "Access-Control-*, Origin, X-Requested-With, Content-Type, Accept");
    header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, HEAD');
    header('Allow', 'GET, POST, PUT, DELETE, OPTIONS, HEAD');
    include('db.php');
    $connect->set_charset("utf8");

        
            $postdata = file_get_contents("php://input");
            $request = json_decode($postdata);
        
                $href = $request['href'];
                $comment = $request['comment'];
                $date = $request['date'];
                $id = $request['id'];
                $photo = $request['image'];

                        
                        
            $create = "INSERT INTO DrugСrime(href, comment, date, user_id, foto) VALUES('$href', '$comment', '$date', '$id', '$photo')";
           
            if($connect->query($create) === TRUE) 
            {
                $message = True;
                $array = array("info" => $message);
                echo json_encode($array);
               
            }
            else
            {
                $message = $connect->error;
                $array = array("info" => $message);
                echo json_encode($array);
            }
        

?>
    

Вот JSON:

{href: "fgdf", comment: "gfdfgd", date: "3-6-2020 15:55", id: "56", image: "data:application/vnd.openxmlformats-officedocument…Qcm9wcy9hcHAueG1sUEsFBgAAAAAPAA8A1AMAAFlHAAAAAA=="}

В результате в БД попадают пустые поля

One Answer

Замени:

$request = json_decode($postdata);

на:

$request = json_decode($postdata,true);

Answered by Maxim in on November 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP