【发布时间】:2012-10-24 22:14:20
【问题描述】:
这个问题非常棘手。当我使用 AFNetworking 通过 IOS 传递 json 数据时。服务器也返回空值。但是,我使用 curl 来测试服务器端,结果是正确的。我不知道这个问题。
这是服务器代码:
$response['return'] = $data;
if (get_magic_quotes_gpc()) {
$data = stripslashes($data);
}
$response['sssss'] = $data;
$data = json_decode($data, TRUE);
$response['return json'] = $data ? $data : 'dddddddd';
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
$response['json error'] = $json_errors[json_last_error()];
$response['p'] = $data->name;
$response['d'] = 'test';
ios 代码:
NSURL *url = [NSURL URLWithString:@"myapi"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:dishParameter,@"dish", nil];
[httpClient postPath:@"dish" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
for (id key in responseObject) {
NSLog(@"key:%@ value:%@", key, [responseObject objectForKey:key]);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error:%@",error);
}];
响应说没有语法错误,服务器可以获取dish参数的值。但是当$data通过stripslashes函数时,$data变成了null。
谁能给我一些建议?
【问题讨论】:
-
你还记得在你的服务器代码中设置 Content-Type: application/json 吗?
-
我使用 forrest bundle 来设置响应。 content-tye 已经设置为 application/json。我使用 curl 将 json 数据发布到服务器。它运作良好。但是通过ios传递的相同数据导致了问题