swift - Swift 4 alamofire responseSerializationFailed
问题描述
我正在尝试使用 alamofire 发送请求 .post 但它给了我这个错误:
responseSerializationFailed(原因:Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误:错误域 = NSCocoaErrorDomain 代码 = 3840 “字符 0 周围的值无效。” UserInfo = {NSDebugDescription = 字符 0 周围的值无效。}))
我在邮递员那里试过,它工作正常,但在代码中给了我那个错误,我使用的是 swift 4.2
class func sendMsg(toUserId:String ,message: String, completion: @escaping (_ error: Error?, _ success: Bool)->Void ) {
//let url = URLs.sendMessage
let url = "http://tdlly.com/Api/SendMessage/18"
let paramaters = [
"to_user_id": toUserId,
"message_content": message,
]
Alamofire.request(url, method: .post, parameters: paramaters, encoding: URLEncoding.default, headers: nil).responseJSON { response in
switch response.result {
case .failure(let error):
completion(error, false)
print(error)
case .success(let value):
let data = JSON(value)
print(data)
if (data["success"].int == 1) {
SVProgressHUD.show(UIImage(named: "cor.png")!, status: "message Sent")
SVProgressHUD.setShouldTintImages(false)
SVProgressHUD.setImageViewSize(CGSize(width: 40, height: 40))
SVProgressHUD.setFont(UIFont.systemFont(ofSize: 20.0))
SVProgressHUD.dismiss(withDelay: 2.0)
} else if (data["success"].int == 0) {
SVProgressHUD.show(UIImage(named: "er.png")!, status: "message not Sent")
SVProgressHUD.setShouldTintImages(false)
SVProgressHUD.setImageViewSize(CGSize(width: 40, height: 40))
SVProgressHUD.setFont(UIFont.systemFont(ofSize: 20.0))
SVProgressHUD.dismiss(withDelay: 2.0)
} else if (data["success"].int == 2) {
SVProgressHUD.show(UIImage(named: "emp.png")!, status: "message empty")
SVProgressHUD.setShouldTintImages(false)
SVProgressHUD.setImageViewSize(CGSize(width: 40, height: 40))
SVProgressHUD.setFont(UIFont.systemFont(ofSize: 20.0))
SVProgressHUD.dismiss(withDelay: 2.0)
}
completion(nil ,true)
}
}
}
解决方案
推荐阅读
- r - 将参数传递给 R 环境中的函数,(gWidget)
- flutter - 嵌套 Transform.scale 和 Transform.translate 时的不同行为
- javascript - 如何为formik字段数组设置初始值?
- javascript - 在 js 的嵌套对象中向上走几级
- mysql - 是否可以从我的表(Sql)中的另一台服务器(Mysql)获取表?
- php - 是否可以在弹性搜索索引上使用 AJAX 和 PHP 实现实时搜索
- linux - 在 ssh 语句中使用 for 循环将变量替换为空字符串
- java - 扑克牌程序有问题吗?(Java)
- svn - Apache SVN 服务器用户管理
- python - Python - 询问输入时的白色图