php - AFNetworking 收到服务器未发送的状态代码

标签 php ios objective-c afnetworking

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

苹果

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 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

我做错了什么?

最佳答案

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

关于php - AFNetworking 收到服务器未发送的状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18450068/

相关文章:

php - 如何在 PHP 中更轻松地将整数值映射到字符串?

iphone - 适用于 iOS 的富文本编辑器库

ios - MapKit 的点击手势

ios - 文本字段应该更改范围内的字符,只有第一个正在执行

ios - 音乐转换以及如何知道是否写入完成

php - NOW() for DATETIME InnoDB Transaction guaranteed?

php - 如何使用 CSS 和 jQuery 创建带有 span 或 ul li 的简单星级评分?

PHP将所有参数作为数组获取?

ios - objective-C/iOS : PopOver

objective-c - ObjC中圆括号和大括号括起来的几个语句的赋值