2013-08-26 36 views
0

我有一个基本的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

我在做什么错?

+0

PHP /服务器日志中是否有任何有关响应的内容? –

+0

另外,您是否能够在浏览器中测试请求?我无法在[PHP文档](http://us3.php.net/manual-lookup.php?pattern=sendResponse&lang=en&scope=quickref)中找到'sendResponse'。 –

+1

我用curl对它进行了测试,它返回“Event Deleted”curl -v -H“Accept:application/json”-H“Content-type:application/json”-X POST -d'{“event_id”:[ 420“]}'http:// host.php' –

回答

2

500是内部服务器错误的HTTP状态码。问题完全在于你的php脚本如何回应请求。

+0

要尝试纠正我的PHP脚本,然后发布我的解决方案时,我有一个 –

相关问题