我有这个功能工作正常(如果你有微调,请随意!):
func httpPostRequest(urlString: String, dataToPost: Dictionary<String, String>) {
let url = URL(string: urlString)!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: dataToPost, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
print("error=\(error) AND error = nil !")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // Check for http(s) errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
return
}
guard let data = data, error == nil else { // Check for fundamental networking error
print("error=\(error)")
return
}
do {
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print("JSON = ")
print(json)
}
} catch let error {
print(error.localizedDescription)
return
}
})
task.resume()
}
PHP 服务器返回这个 JSON 字符串:
{'exception': false, 'success': false, 'status': -8, 'message': 'Your email address is not valid !', 'confirmMessage': 'null', 'html': 'null', 'data': 'null'}
这是 XCode 控制台中显示的内容:
JSON =
["status": -8, "data": null, "html": null, "message": Your email address is not valid !, "exception": 0, "confirmMessage": null, "success": 0]
我需要返回此 JSON 字符串以继续处理此数据。
如何转换我的函数来执行此操作?
最佳答案
这应该是函数。
func httpPostRequest(urlString: String, dataToPost: Dictionary<String, String>, completionHandler:@escaping (Dictionary<String, Any>) -> ()) {
let url = URL(string: urlString)!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: dataToPost, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
print("error=\(error) AND error = nil !")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // Check for http(s) errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
return
}
guard let data = data, error == nil else { // Check for fundamental networking error
print("error=\(error)")
return
}
do {
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print("JSON = ")
print(json)
completionHandler(json)
}
} catch let error {
print(error.localizedDescription)
return
}
})
task.resume()
}
关于php - Swift 3 如何转换此函数以返回 json 变量的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43103329/