我有一個基本的php腳本代碼,它接收JSON POST併發送一個http狀態代碼。AFNetworking接收未由服務器發送的狀態碼
PHP成功接收JSON併發送http200狀態碼,但不知何故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
我在做什麼錯?
PHP /服務器日誌中是否有任何有關響應的內容? –
另外,您是否能夠在瀏覽器中測試請求?我無法在[PHP文檔](http://us3.php.net/manual-lookup.php?pattern=sendResponse&lang=en&scope=quickref)中找到'sendResponse'。 –
我用curl對它進行了測試,它返回「Event Deleted」curl -v -H「Accept:application/json」-H「Content-type:application/json」-X POST -d'{「event_id」:[ 420「]}'http:// host.php' –