2013-08-26 2 views
0

JSON POST를 수신하고 http 상태 코드를 다시 보내는 기본 PHP 스크립트 코드가 있습니다.AFNetworking은 서버에서 보내지 않은 상태 코드를 수신합니다.

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]; 
,536,913,632 10

는 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

PHP/서버 로그에 응답이 있습니까? –

+0

또한 브라우저에서 요청을 테스트 할 수 있었습니까? [PHP docs] (http://us3.php.net/manual-lookup.php?pattern=sendResponse&lang=en&scope=quickref)에서'sendResponse'를 찾을 수 없습니다. –

+1

"curl -v -H"를 반환하는 컬 (curl)로 테스트했습니다. curl -v -H "application-json"-H "컨텐츠 유형 : application/json"-X POST -d '{ "event_id": [ " 420 "]} 'http : // host.php' –

답변

2

500은 내부 서버 오류에 대한 HTTP 상태 코드입니다. 문제는 전적으로 귀하의 PHP 스크립트가 요청에 응답하는 방법입니다.

+0

그런 다음 내 PHP 스크립트를 수정하려고하고 내 솔루션을 게시 할 때 게시하십시오. –

관련 문제