【发布时间】:2011-03-17 10:50:34
【问题描述】:
在过去的 5 个小时里,我一直在尝试将 json 对象发布到 PHP 脚本中。我已经阅读了所有文档,似乎代码应该可以工作,但不能。请求已发出并接收正常,但我无法访问发布的 json 数据,甚至不知道它是否已正确发送。我不断得到一个空的 php 数组,而不是 json 数据。
NSString *jsonString = [self.tableDataSource JSONRepresentation];
NSURL *url = [NSURL URLWithString:@"http://myserver.com/test.php"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
这里是php脚本(test.php)
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
print_r($decoded);
我正在使用来自https://github.com/pokeb/asi-http-request.git 的最新 ASIHttpRequest 代码
我做错了什么??????
干杯, 旅游。
【问题讨论】:
-
这需要基本的调试。我没有 iPhone 开发经验,但必须有可以显示成功状态和请求响应正文的监控工具。 PHP端可以激活
error_reporting(E_ALL);输出任何可能出现的问题。 -
您不需要访问 $_POST[] 变量吗?
-
嗨 Pekka,当我打开 error_reporting(E_ALL) 时没有返回任何内容。你有更多的想法吗?
-
嗨,Les,我试过 print_r($_POST) 和 print_r($_GET);我仍然无法获得任何 json 数据。还有什么想法吗?
标签: php iphone json ios asihttprequest