Alamofire - 字符0周围的值无效

时间:2018-04-20 13:33:05

标签: php json swift alamofire

我使用Alamofire获得了以下错误。想知道我的服务器代码有什么问题。如果我使用responseString,我会得到一个显示整个PHP代码的结果,包括<?php?>。有什么建议吗?

Alamofire.request("http://domain/test.php", method: .post, parameters: [:], encoding: URLEncoding.default, headers: [:]).responseJSON { (response) in
            switch response.result {
            case .success(_):
                print(response.result.value)
            case .failure(let error):
                print(error)
            }
        }

错误

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

服务器代码(test.php)

<?php

$essay = 
array(
    array("Intro" => 
        array("intro")
        ),
    array("section" => 
        array("a", "b", "c", "d", "e", "f", "g" )
        ),
    array("End" => 
        array("a", "b")
        )
    );

echo json_encode($essay);

?>

如果我使用responseString,我会得到

Optional("<?php\n\n$essay = \narray(\n\tarray(\"Intro\" => \n\t\tarray(\n\t\t\t\"intro\"\n\t\t\t)\n\t\t),\n\tarray(\"1\" => \n\t\tarray(\n\t\t\t\"a\",\n\t\t\t\"b\",\n\t\t\t\"c\",\n\t\t\t\"d\",\n\t\t\t\"e\",\n\t\t\t\"f\",\n\t\t\t\"g\"\n\t\t\t)\n\t\t),\n\tarray(\"End\" => \n\t\tarray(\t\n\t\t\t\"a\", \n\t\t\t\"b\"\n\t\t\t)\n\t\t)\n\n\t);\n\necho json_encode($essay);\n\n?>\n")

如果我尝试在服务器中运行代码,我会

php test.php 

[{"Intro":["intro"]},{"1":["a","b","c","d","e","f","g"]},{"End":["a","b"]}]

0 个答案:

没有答案