AFNetworking:发送异步数据mde“发布”

时间:2014-02-01 10:22:42

标签: php ios post afnetworking-2

您好我尝试使用AFNetworking从我的服务器注册数据库中的用户,但它不起作用。 以下是IOS代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:@"http://www.myWebSite.com/user/newUser" parameters:dictonaryUser success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

这是PHP代码:

header('Content-type: text/json');
    echo json_encode($_POST);

这里的错误:

2014-02-01 11:07:24.051 myApp[2534:70b] 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=0x8b9af60 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

尝试添加此代码

AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

manager.requestSerializer = requestSerializer;

但如果它不起作用,请参考此link