AFNetworking 2.0 POST问题,Cocoa错误3840(JSON文本没有以数组开头......) [英] AFNetworking 2.0 POST issue, Cocoa error 3840(JSON text did not start with array...)

查看:394
本文介绍了AFNetworking 2.0 POST问题,Cocoa错误3840(JSON文本没有以数组开头......)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的本地服务器上调用api.php(使用MAMP)。
正在调用服务器端api.php,但php代码中的_POST内容包含以下错误:

I am attempting to call api.php on my local server(using MAMP). The server side api.php is being called but the content of _POST inside the php code contains the following 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 = 0x15d7bdd0 {NSDebugDescription = JSON text did not start with array or object and option to allow fragments not set.}

我的应用程序是尝试将JSON POST请求发送到api.php是一个使用AFNetworking 2的iOS应用程序

My app that is trying to send the JSON POST request to the api.php is an iOS app using AFNetworking 2

这是我的请求代码:

- (void)postUpdateRequest
{
    if (!dataModel)
        dataModel = [[DataModel alloc] init];

    NSDictionary *params = @{@"foo": @"bar2"};

    NSLog(@"%@",params);

/*    NSDictionary *params = @{@"cmd":@"update",
                             @"user_id":[dataModel userId],
                             @"token":[dataModel deviceToken]
                             };//@"ip_address":[dataModel getIPAddress]};*/

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager POST:ServerApiURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

我查看了整个互联网,但无法找到我的具体答案问题。
无论我做什么,我都会收到相同的错误。

I looked all over the internet but can't find an answer for my specific problem. No matter what I do I get the same error back.

为什么我的JSON格式不正确?我似乎也找不到嗅探我实际发送的JSON的方法。

Why is my JSON not properly formatted? I also can't seem to find a way to sniff what JSON I am actually sending.

谢谢。

推荐答案

当您在示例中使用API​​时,您的HTTP消息将使用Content-Type application / x-www-form-urlencoded 。在内部,你的 param 字典由AFN编码(虽然不是严格按w3c规定的正确),并设置为请求正文。

When you use the API as in your example, your HTTP message will be composed by using a Content-Type application/x-www-form-urlencoded. Internally, your param dictionary are encoded by AFN (although not strictly correct as specified by w3c), and set as the request body.

由于您没有指定 Accept 标头,服务器可以自由选择可能的响应数据的内容类型(如果有的话)。

Since, you did not specify an Accept header, the server is free to choose the content type for a possible response data (if any).

当您收到回复时,您需要始终检查HTTP状态代码和响应正文的内容类型(如果有)。

When you receive a response, you need to always check the HTTP status code and the content type of a response body (if any).

很可能,服务器返回了一个状态代码,表明存在某些问题,并且响应正文包含某种不同类型的内容类型的错误响应(例如,它返回) text / html)。

It's likely, the server returned a status code indicating some issue and a response body containing an "error response" in some different kind of content type than you expect (for example, it returned text/html).

这篇关于AFNetworking 2.0 POST问题,Cocoa错误3840(JSON文本没有以数组开头......)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆