我在 Node.js 中向 php 服务器发送 POST 请求。在请求中,我将 json 作为对象而不是字符串包含在内。
Node.js 请求:
var request = require('request');
let data = { "name":"John", "age":30, "car":null };
request.post({
headers: {'content-type' : 'application/json; charset=utf-8'},
url: 'http://0.0.0.0:9000/html.php',
method: 'POST',
json: data
}, function(error, response, body){
console.log(body);
});
html.php:
<?php
# Get JSON as an object
$json = file_get_contents('php://input');
$name = $json->name;
echo $json // Prints out the whole json correctly
echo $name; // Prints out "undefined"
我希望输出是“John”而不是“undefined”
月关宝盒
桃花长相依