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"]}]