node.js — чтение объекта JSON в php, отправленного с Nodejs с помощью модуля Fetch

Я пытаюсь опубликовать объект JSON из Nodejs на сервер LAMP.
Я могу отправить объект, но не могу получить его из своего php-скрипта.
Мне нужна помощь в получении объекта в сценарии php.

Всегда возвращайся — НЕ УСТАНОВЛЕНО —
ЗАЧЕМ ?
Как мне это сделать ?
Благодарю вас

Мой код:

**In express**

module.exports = function(app){
var fetch = require('node-fetch');
var request = require('request-promise');

//Save
app.post('/cajaSave', function(req, res)
{
var pago = req.body;  //object to send

fetch('http://www.myurl/myscript.php',{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify(pago)
})
.then(function(res) {
return res.json();
})
.then(function(json) {
console.log(json);
})
.catch(function(err) {
console.log(err);
});
});

My php code is:

<?php
header('Access-Control-Allow-Origin: *');
$json = $_POST['body'];
if(isset($_POST['body']))
{
$msg = "set";
}
else
{
$msg = "not set";
}
echo json_encode($msg);
?>

1

Решение

JSON сбрасывается в php://input

$_POST = json_decode(file_get_contents('php://input'), true);
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector