gpt4 book ai didi

swift - Alamofire 无法通过 php 获取请求

转载 作者:行者123 更新时间:2023-11-30 12:17:00 28 4
gpt4 key购买 nike

我有这个 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)
)

最佳答案

试试这个代码

   let par: Parameters = [
"usn":"Murad",
"Password":"monkey"
]


Alamofire.request("http://www.web.com/ajax/logreg.php", method: .post, parameters: par as? [String : AnyObject], encoding: JSONEncoding.default).responseJSON { response in
switch response.result {
case .success(let JSON):
// print("Success with JSON: \(JSON)")
if let response = JSON as? NSMutableDictionary
{
callback(response,nil)
}
else if let response = JSON2 as? NSDictionary
{
callback(response,nil)
}
else if JSON2 is NSArray
{


}
else if ((JSON as? String) != nil)
{

let userDic : [String : String] = ["quatid":JSON as! String]
print(userDic)
callback(userDic as NSDictionary?,nil)

}

break

case .failure(let error):
print("Request failed with error: \(error)")
callback(response.result.value as? NSMutableDictionary,error as NSError?)
break
}
}
.responseString { response in
print("Response String: \(response.result.value)")
}

关于swift - Alamofire 无法通过 php 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45300766/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com