有没有人让JSON和TIdHttp一起工作?
PHP总是在$_POST
中返回NULL,我做错了什么吗?
Delphi来源:
http := TIdHttp.Create(nil);
http.HandleRedirects := True;
http.ReadTimeout := 5000;
http.Request.ContentType := 'application/json';
jsonToSend := TStringStream.Create('{"name":"Peter Pan"}');
jsonToSend.Position := 0;
Memo1.Lines.Text := http.Post('http://www.website.com/test.php', jsonToSend);
jsonToSend.free;
http.free;
PHP源码:
<?php
$value = json_decode($_POST);
var_dump($value);
?>
发布于 2012-08-30 15:20:03
您不能使用TStringList
来发布JSON数据。TIdHTTP.Post()
将以破坏TStringList
数据的方式对JSON内容进行编码。您需要将JSON数据放入TStream
中。TIdHTTP.Post()
将按原样传输其内容。此外,不要忘记设置TIdHTTP.Request.ContentType
属性,以便服务器知道您正在发布JSON数据。
发布于 2012-08-29 11:51:23
你需要定义一个post变量,试试这段代码(我已经在你的代码中添加了"json“var ):
Delphi代码:
http := TIdHttp.Create(nil);
http.HandleRedirects := true;
http.ReadTimeout := 5000;
jsonToSend := TStringList.create;
jsonToSend.Text := 'json={"name":"Peter Pan"}';
Memo1.Lines.Text := http.Post('http://www.website.com/test.php', jsonToSend);
jsonToSend.free;
http.free;
PHP源码:
<?php
$value = json_decode($_POST['json']);
var_dump($value);
?>
https://stackoverflow.com/questions/12175952
复制相似问题