【问题标题】:Connecting JSON AFnetworking 2.0连接 JSON AFnetworking 2.0
【发布时间】:2014-08-31 14:37:33
【问题描述】:

我正在开发 iOS 应用程序,我在使用 AFnetworking 2.0 从客户端接收数据到服务器时遇到问题。 Json,以下是我在 iOS 上使用 post 方法的代码,请帮助!

我需要服务器端代码(php),如何获取数据

在 iOS 上发帖

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *params = @ {@"user" :@"Akhtar", @"pwd" :@"123" };
[manager POST:@"URl" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
{
    NSLog(@"JSON: %@", responseObject);
    [self ResponseHandlingWithResponseData:responseObject WithTag:tag];
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
    NSLog(@"Error: %@", error);
    [self errorResponseHandlingWithResponseDict:error.userInfo WithTag:tag];
}];

【问题讨论】:

    标签: php ios json afnetworking afnetworking-2


    【解决方案1】:

    试试这段代码

    <?php
    header('Content-type: application/json');
    
    $isPostRequest = $_SERVER['REQUEST_METHOD'] == 'POST';
    
    if ($isPostRequest)
    {
        $jsonString = file_get_contents('php://input');
        $jsonArray = json_decode($jsonString, true);
    
        $response = ["status" => "ok"];
    
        echo json_encode($jsonArray);
    
    }
    
    
    ?>
    

    【讨论】:

      【解决方案2】:

      PHP 代码将是:

      $var = json_decode(file_get_contents('php://input'), true);
      echo '{}';
      

      【讨论】:

      • 嗨,感谢您的代码,但它不适用于我,这是错误日志 2014-09-01 11:03:20.127 MEMC[875:60b] 错误:错误域 = NSCocoaErrorDomain 代码 = 3840 “操作无法完成。(Cocoa 错误 3840。)”(JSON 文本未以数组或对象开头,并且允许未设置片段的选项。) UserInfo=0xa288860 {NSDebugDescription=JSON 文本未以数组或对象开头和允许未设置片段的选项。}
      • 对不起,错误是因为 iOS 期望 json 响应。我编辑了答案
      猜你喜欢
      • 2013-11-28
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多