douzhi19900102 2018-04-20 13:33
浏览 145

Alamofire - 字符0周围的值无效

I am getting below error by using Alamofire. Wondering what is wrong with my server code. If I use responseString, I get a result showing the whole php code including <?php and ?>. Any suggestions?

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)
            }
        }

Error

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

Server Code (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);

?>

If I use responseString, I get

Optional("<?php

$essay = 
array(
\tarray(\"Intro\" => 
\t\tarray(
\t\t\t\"intro\"
\t\t\t)
\t\t),
\tarray(\"1\" => 
\t\tarray(
\t\t\t\"a\",
\t\t\t\"b\",
\t\t\t\"c\",
\t\t\t\"d\",
\t\t\t\"e\",
\t\t\t\"f\",
\t\t\t\"g\"
\t\t\t)
\t\t),
\tarray(\"End\" => 
\t\tarray(\t
\t\t\t\"a\", 
\t\t\t\"b\"
\t\t\t)
\t\t)

\t);

echo json_encode($essay);

?>
")

If I try to run the code in my server, I get

php test.php 

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

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型