0

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

我究竟做错了什么?

4

1 回答 1

2

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

于 2013-08-26T21:32:11.850 回答