typescript 将 JSON 从 angular 2 发布到 php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35325816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:16:31  来源:igfitidea点击:

Post JSON from angular 2 to php

phpjsontypescriptangular

提问by daniel

I try to post data from angular 2 to php:

我尝试将数据从 angular 2 发布到 php:

let headers = new Headers();
headers.append('Content-Type', 'application/json');
var order = {'order': this.orders};

this.http.post('http://myserver/processorder.php', JSON.stringify(order), {
    headers: headers
}).subscribe(res => {
    console.log('post result %o', res);
});

In angular 2 one can only post string as data and not Json? That's ok for me but I struggle to get the posted data on php. I tried $obj = $_POST['order'];

在 angular 2 中,只能将字符串作为数据而不是 Json 发布?这对我来说没问题,但我很难在 php 上获取发布的数据。我试过$obj = $_POST['order'];

采纳答案by inki

Marc B is correct, however what is happening is that the $_POST array will contain an empty value with a key set to the JSON string you are passing...

Marc B 是正确的,但是发生的情况是 $_POST 数组将包含一个空值,其键设置为您传递的 JSON 字符串...

Array
(
    [{"order":"foobar"}] => 
)

You "can" grab that (although this would be the wrong approach) by getting the key using...

您“可以”通过使用...获取密钥来抓住它(尽管这将是错误的方法)

key($_POST)

for example:

例如:

$obj = json_decode(key($_POST));
echo $obj->order;

BUTwhat you can do is send the data as value key pairs:

但是您可以做的是将数据作为值键对发送:

let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
let order = 'order=foobar';

this.http.post('http://myserver/processorder.php', order, {
    headers: headers
}).subscribe(res => {
    console.log('post result %o', res);
});

Then in PHP you can grab the data using:

然后在 PHP 中,您可以使用以下方法获取数据:

$_POST['order']

Few things to note:

需要注意的几点:

  • changed header Content-Type to application/x-www-form-urlencoded (more for my own testing since this doesn't do any preflight requests)
  • notice that orderis a key value pair string instead of JSON
  • notice that orderin this.http.post is getting passed as-is without JSON.stringify
  • 将标题 Content-Type 更改为 application/x-www-form-urlencoded (更多用于我自己的测试,因为这不会执行任何预检请求)
  • 注意order是一个键值对字符串而不是 JSON
  • 请注意this.http.post中的顺序在没有 JSON.stringify 的情况下按原样传递

回答by Gabriel Martins

I do not know if it's bad practice but it seems right to me, although it bothers me a little

我不知道这是否是不好的做法,但对我来说似乎是正确的,虽然它让我有点困扰

const headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

const obj = { nome: 'gabriel', age: 20 };
const body = 'data=' + JSON.stringify(obj);

this.http.post('/test.php', body, { headers })
  .subscribe(res => console.log(res.json()), res => console.error(res))

And in php

在 php 中

$post = json_decode($_POST['data']);

回答by Thierry Templier

Agreed with you that we can't at the moment provide object instead of string. It's a feature in progress. See this issue:

同意您的意见,我们目前无法提供对象而不是字符串。这是一个正在进行中的功能。看到这个问题:

Regarding your problem to get JSON data on the server side, this question should help you:

关于您在服务器端获取 JSON 数据的问题,这个问题应该可以帮助您: