我从php服务器得到Null响应

时间:2012-10-15 13:26:26

标签: iphone objective-c ios nsurlrequest

NSURL *URL = [NSURL     URLWithString:@"http://"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"POST";

NSString *params = @"json=deepak";
NSData *data = [params dataUsingEncoding:NSUTF8StringEncoding];


[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%i", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];


NSURLResponse *responce;
NSError *error;
NSData *returnData = [ NSURLConnection sendSynchronousRequest:request returningResponse:&responce error:&error];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"responseData: %@", content);

大家好,我正在使用stackOverflow本身的代码并且编写的答案正在运行,但我得到了

来自php服务器的“response data = null”

我的简单服务器代码是

$ json = CJSON :: decode($ _ POST ['json'],TRUE); echo CJSON :: encode($ json);

1 个答案:

答案 0 :(得分:0)

是的,我终于解决了我的问题.....       为了使密钥对值,我必须以数组方式发送数据。即

 @"{\"user\":\"deepak\",\"password\":\"1234\"}";

这是json键的值。

然后你可以使用

在php服务器上解码

$ json = CJSON :: decode($ _ POST ['json'],TRUE); echo CJSON :: encode($ json);

就像这样,它就像魅力一样