【问题标题】:Problems retrieving JSON from PHP server using Alamofire使用 Alamofire 从 PHP 服务器检索 JSON 的问题
【发布时间】:2015-10-14 22:17:52
【问题描述】:

当我发送一个数组时,我在服务器中检索到错误的 JSON:

客户端对象:

class Dummy : Mappable {
    var test1 = "test1";
    var test2 = "test2";

    func mapping(map: Map) {
        test1 <= map["test1"]
        test2 <= map["test2"]
    } 
    required init(){}
}

客户调用:

let wrongDummies : [Dummy] = [Dummy(), Dummy()];
let wrongDummiesJSONArray = Mapper().toJSONArray(wrongDummies)
let dummies : [String:AnyObject] = [

    "right": Mapper().toJSON(Dummy()),

    "again_right": ["dummy1" : Mapper().toJSON(Dummy()), "dummy2" : Mapper().toJSON(Dummy())],

    "wrong": [Mapper().toJSON(Dummy()), Mapper().toJSON(Dummy())],

    "again_wrong": wrongDummiesJSONArray

]
println(dummies)
request(.POST, PROFILE_URL, parameters: dummies)

客户端打印(看起来还可以):

[right: {
    test1 = test1;
    test2 = test2;
}, wrong: (
        {
        test1 = test1;
        test2 = test2;
    },
        {
        test1 = test1;
        test2 = test2;
    }
), again_right: {
    dummy1 =     {
        test1 = test1;
        test2 = test2;
    };
    dummy2 =     {
        test1 = test1;
        test2 = test2;
    };
}, again_wrong: (
        {
        test1 = test1;
        test2 = test2;
    },
        {
        test1 = test1;
        test2 = test2;
    }
)]

服务器实现(PHP):

ini_set("log_errors", 1);
ini_set("error_log", "$root/php-error.log");
error_log(print_r($_POST, true));

响应服务器:

Array
(
    [again_right] => Array
        (
            [dummy2] => Array
                (
                    [test2] => test2
                    [test1] => test1
                )

            [dummy1] => Array
                (
                    [test2] => test2
                    [test1] => test1
                )

        )

    [again_wrong] => Array
        (
            [0] => Array
                (
                    [test2] => test2
                )

            [1] => Array
                (
                    [test1] => test1
                )

            [2] => Array
                (
                    [test2] => test2
                )

            [3] => Array
                (
                    [test1] => test1
                )

        )

    [right] => Array
        (
            [test2] => test2
            [test1] => test1
        )

    [wrong] => Array
        (
            [0] => Array
                (
                    [test2] => test2
                )

            [1] => Array
                (
                    [test1] => test1
                )

            [2] => Array
                (
                    [test2] => test2
                )

            [3] => Array
                (
                    [test1] => test1
                )

        )

)

如您所见,数组中的对象按其属性的数量进行分割,而字典中的对象不会发生这种情况。

【问题讨论】:

    标签: php ios swift alamofire


    【解决方案1】:

    在您的日志中,我们看到了来自 iOS 端和 PHP 端的数据,但我们无法确定实际传输的是什么。我们必须看看问题实际发生在哪一边,所以看看你是否可以打印出正在发送的实际 HTTP 请求。如果JSON错了,那么iOS端就错了,否则PHP就是读错了。

    首先,确保将参数作为 JSON 发送到服务器。默认情况下,Alamofire 将默认使用 URL 编码,除非您指定要使用 JSON。

    接下来,您似乎正在打印出$_POST,这表明您可能将数据作为 URL 编码参数而不是 JSON 发送。那可能是搞砸了。

    如果 Alamofire 以 JSON 格式发送数据,而您在服务器上没有收到任何数据,请确保将其更改为读取包含 JSON 数据的 HTTP 正文:

    $inputJSON = file_get_contents('php://input');
    $input= json_decode( $inputJSON, TRUE );
    

    我认为这可能应该涵盖你的基础,或者至少给你一个好的开始,看看哪里:)

    【讨论】:

    • file_get_contents('php://input');是的,当我将编码更改为 JSON 时,我会用它来获取数据。谢谢你
    • 顺便说一句,什么可能导致我可以检索在 POST 上以 .JSON 形式发送的内容,但我可以使用 file_get_contents 检索它?
    • 将其作为.JSON 发送为实际的 JSON 格式,而不是 URL 编码的形式——这就是搞砸原始 JSON 的原因。当您使用file_get_contents 获取数据时,您正在读取实际的 HTTP 请求正文,而不仅仅是参数 - 将其更改为 JSON 更改了数据在 HTTP 请求中的存储位置。
    猜你喜欢
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2020-11-27
    相关资源
    最近更新 更多