我有一个基本的PHP脚本代码,它接收JSON POST并发回一个http状态代码。
PHP成功接收JSON并发送http 200状态代码但不知何故AFNetworking获取Expected status code in (200-299), got 500
,在php脚本中我定义了500状态代码但我从不使用/发送该状态代码,我一直在寻找PHP脚本和再一次,它没有发送正确的代码200
,但AFNetworking会将其收到500
PHP:
//send delete request here
$query ="DELETE FROM OWN_EVENTS WHERE event_id IN ('$names')";
$result = $sql->query($query);
//printf("$query: %s\n", $query);
//var_dump($query);
//printf("\n");
if (!$result) {
//var_dump($result);
//printf("\n");
//printf("Query failed: %s\n", $mysqli->error);
sendResponse(417, json_encode("Query failed"));
exit;
}
sendResponse(200, json_encode("Event Deleted"));
IOS
NSError* error;
NSMutableDictionary *nameElements = [[NSMutableDictionary alloc] init];
[nameElements setObject:saveArray forKey:@"event_id"];
NSData *result =[NSJSONSerialization dataWithJSONObject:nameElements options:0 error:&error];
NSString *displayJson = [[NSString alloc] initWithData:result
encoding:NSUTF8StringEncoding];
NSLog(@"saveArray result %@",displayJson);
NSURL *url = [NSURL URLWithString:server];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
// don't forget to set parameterEncoding!
httpClient.parameterEncoding = AFJSONParameterEncoding;
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:deleteEventInDatabase parameters:nil];
[request setHTTPBody:[displayJson dataUsingEncoding:NSUTF8StringEncoding]];
[request addValue:@"ASIHTTPRequest" forHTTPHeaderField:@"User-Agent"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSLog(@"request %@",request);
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:nil failure:nil];
operation.JSONReadingOptions = NSJSONReadingAllowFragments;
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) {
// Print the response body in text
if (operation.response.statusCode == 200) {
NSLog(@"Events Sucessfully Deleted");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
if (operation.response.statusCode == 500) {
NSLog(@"Internal Server Error");
//remove this when u fix it
}
}];
[operation start];
ERROR:
Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 500" UserInfo=0x11c8c9d0 {NSLocalizedRecoverySuggestion="Event Deleted", AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest http://host.php>, NSErrorFailingURLKey=http://host.php, NSLocalizedDescription=Expected status code in (200-299), got 500, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xa9aba80>
正如您所看到的那样,它显示NSLocalizedRecoverySuggestion="Event Deleted"
所以php确实发送了200
我做错了什么?
答案 0 :(得分:2)
500是内部服务器错误的HTTP状态代码。问题完全在于你的php脚本如何响应请求。