ASIHTTPRequest 将 json 发送到 php 服务器
在过去的 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
我做错了什么???
干杯, 旅行。
I have been trying to post a json object to a PHP script for the last 5 hours. I have read all the docs and it seems the code should work BUT it does not. The request is made and received ok, but I can't access the posted json data or dont even know if its been sent correctly. I keep getting an empty php array, instead of the json data.
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];
here is the php script (test.php)
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
print_r($decoded);
I am using the lastest ASIHttpRequest code from https://github.com/pokeb/asi-http-request.git
What am I doing wrong?????
Cheers,
Trav.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发现问题了。我在 apache vhost 配置中有一个 SSL 重定向。使用 tcp 数据包嗅探器来找到它。一旦我删除重定向,我就能够接收 json 数据:
感谢所有提供帮助的人。
Found the issue. I had a SSL redirect in the apache vhost config. Used a tcp packet sniffer to find it. once i remove the redirect i was able to receive the json data with:
Thanks to all those who helped.
我相当确定 ASIHTTPRequest 默认为 GET 请求。您正在尝试发布数据,所以我认为这不会起作用。
尝试使用 ASIFormDataRequest,它是为发布数据而构建的。或者,至少将请求方法更改为POST。
I'm fairly sure that ASIHTTPRequest defaults to a GET request. YOu're trying to POST data so I don't think this will work.
Try using ASIFormDataRequest which is built for POSTing data. Or, at the very least change the request method to POST.