this problem is very tricky. when I pass json data through IOS by using AFNetworking. the server also return null value. However, I used curl to test server side, the result is correct. I have no idea about this problem.
Here is server code:
$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 code :
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);
}];
the response said that no syntax error and server can get the value of dish parameter. but when $data through stripslashes function, $data becomes to null.
Anyone can give me some suggestion?
$data
is type string? Why would you strip slashes from JSON? You screw the syntax. Do not do that. json_decode
returns null becauuse it's an invalid JSON format.