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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?