這個問題非常棘手。當我通過使用AFNetworking通過IOS傳遞json數據時。該服務器也返回空值。但是,我用curl來測試服務器端,結果是正確的。我不知道這個問題。json_decode當數據通過ios時返回null
這裏是服務器代碼:
$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);
}];
迴應說,沒有語法錯誤和服務器可以獲得菜參數的值。但是當通過stripslashes運行$ data時,$ data將變爲null。
任何人都可以給我一些建議嗎?
你是否記得在你的服務器代碼中設置Content-Type:application/json? – AndrewR
我用forrest包來設置響應。 content-tye已經設置爲application/json。我用curl將json數據發佈到服務器。它運作良好。但通過ios傳遞的相同數據導致問題 – leoyfm