0

jsonデータをサーバーに送信し、サーバーから正しいresponse.statusCodeを取得したいAFNetworking

サーバ:

// Helper method to send a HTTP response code/message
function sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
    $status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
    header($status_header);
    header('Content-type: ' . $content_type);
    echo $body;
} 
sendResponse(200, json_encode("Files Deleted"));

IO:

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:deleteExtraFilesIn 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"]];
        AFHTTPRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:nil failure:nil];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) {
            // Print the response body in text
            if (operation.response.statusCode == 200) {
                NSLog(@"Got you");

            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
        }];
        [operation start];

エラー NSLOG:

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x1f519df0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

上記のコードで正しい http 応答を取得するにはどうすればよいですか?

注:json_encode("Files Deleted")サーバー上では、http応答コードが必要なダミーテキストです。

4

1 に答える 1

1

フラグメントを許可する操作を設定する必要があります。

        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:nil failure:nil];
operation.JSONReadingOptions = NSJSONReadingAllowFragments;
于 2013-06-12T22:03:58.217 に答える