简体   繁体   English

PHP中的JSON发布方法

[英]JSON post method in php

I don't get what do I do wrong when I try to get my values from post method from angular to php, it seems that everything is correct but then I am not able to see any values to postman/return the correctly. 当我尝试从angular到php的post方法获取值时,我没有弄错我怎么做,看来一切都正确,但是后来我看不到任何值来邮递员/正确返回。 这就是我得到的

this is my php method with all my attempts: 这是我所有尝试的php方法:

public function created()
{
    $json = file_get_contents('php://input');
    $json_string_in_array = array($json);
    $json_array =json_decode($json_string_in_array[0]);
    var_dump($json_string_in_array);
    var_dump($json_array);
    //return $json_array;
  //  $data = json_decode(file_get_contents('php://input'), true);
   // $data = json_encode(file_get_contents("php://input"));
  //  $request = json_decode($data);
  //  var_dump($data);
  //  return $data;
  //  return $request;
  //  $json = file_get_contents('php://input');
    //$obj = json_decode($json);
     // get the raw POST data
    // $rawData = file_get_contents("php://input");
    // this returns null if not valid json
    // return response()->json($rawData);
    //  DatabaseModel::DatabaseModel($json_array);
}

what I am trying to do is get that data and then send it to DatabaseModel in order to add it to database. 我想做的是获取该数据,然后将其发送到DatabaseModel以便将其添加到数据库。

First you header, content-type , should be application/json . 首先,您的标头content-type应该是application/json Are you manually setting the headers in angular? 您是否手动将标题设置为有角度? I am pretty sure angular will detect (or at least it is the default) the content type if its not set so in your case it would be application/json 我很确定angular如果没有设置内容类型,它将检测(或至少是默认)内容类型,因此在您的情况下为application/json

<?php
$json = file_get_contents('php://input');
$request = json_decode($json);
$name = $request->name;
$email = $request->email;
# etc
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM