【发布时间】:2017-07-25 10:34:38
【问题描述】:
我有这个 Swift 4 代码
let par: Parameters = [
"usn":"Murad",
"Password":"monkey"
]
Alamofire.request("http://www.web.com/ajax/logreg.php",method: .post,parameters:par,encoding: URLEncoding.httpBody).response { response in
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)")
}
}
还有这个 PHP 代码
print_r($_POST);
现在我想这段代码会打印出来
Array(
"usn":"Murad",
"Password":"monkey",
)
但它会打印
Array
(
[(componentTuple_0)] => (componentTuple.1)
)
【问题讨论】:
-
更改编码试试?
-
看起来像 JSON,你确定你的 API 没有返回 JSON?
-
@DávidPásztor Json encoding 给了我这个错误
fAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) [Timeline]: Timeline: { "Request Start Time": 522672231.598, "Initial Response Time": 522672231.719, "Request Completed Time": 522672231.720, ... -
JSON 请求无法接收到
$_POST。如果你想让你的 Swift 代码正常工作,你可能需要更新你的 PHP 代码。 -
@OOPer 但在文档中 github.com/Alamofire/Alamofire 它说你应该像我一样发送帖子。