import UIKit
import Alamofire
import SwiftyJSON
class LoginViewController: UIViewController {
@IBOutlet weak var urlTextFiled: UITextField!
@IBOutlet weak var emailTextFiled: UITextField!
@IBOutlet weak var passwordTextFiled: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
guard let email = emailTextFiled.text, !email.isEmpty else {
return
}
guard let password = passwordTextFiled.text, !password.isEmpty else {
return
}
let db = "mohanad"
let url = "http://176.58.117.249/web/session/authenticate"
let parameters: [String: Any] = [
"db": db,
"login": email,
"password": password ,
]
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil)
.validate(statusCode: 200..<600)
.responseJSON { response in
switch response.result
{
case .failure(let error):
print(error)
case .success(let value):
print(value)
}
}
}
}
打印以下错误:
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域= NSCocoaErrorDomain代码= 3840&#34;字符0周围的值无效。&#34; UserInfo = {NSDebugDescription =字符0周围的值无效。})
答案 0 :(得分:5)
将.responseJSON
替换为.responseString
,以便:
而不是:
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON
{ response in
//....
}
做的:
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseString
{ response in
//....
}
答案 1 :(得分:0)
试试这个
Paramters:
let parameters: [String: Any] = [
"jsonrpc":"2.0",
"method":"call",
"params": [
"db": db,
"login": "mohanad@amana.ps",
"password": "mohanad123",
]
]
请求。使用encoding: JSONEncoding.default
,因为您已传递原始值。
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil)
.validate(statusCode: 200..<600)
.responseJSON { response in
switch response.result
{
case .failure(let error):
if let data = response.data {
print("Print Server Error: " + String(data: data, encoding: String.Encoding.utf8)!)
}
print(error)
case .success(let value):
print(value)
}
}
你的api在这个
中给我这种类型的数据"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad Request</title>\n<h1>Bad Request</h1>\n<p>Session expired (invalid CSRF token)</p>\n"