Jam*_*s H 14 xcode json swift alamofire swift2
一直在研究这个没有成功.我有一个函数,UIButton
仅用于执行对使用所有JSON的rails api的alamofire调用.
我正在使用Swift 2,Alamofire 3,XCode 7和Rails 4为我的api部署到Heroku
当我关闭该功能时,我不断收到此错误:
alamofire.error Code = -6006"JSON无法序列化.输入数据为零或零长度.
这是我的代码:
@IBAction func Save(sender: AnyObject) {
let postsEndpoint: String = "https://APIURL"
let parameters = [
"users": [
"name": "James McHarty",
"avatar": "Some binary data",
"post": [
"title": "First Test Post",
"body": "This is the first test post for the API",
"liked": "8", //will make INT later
"img": "more binary data"
]
]
]
Alamofire.request(.POST, postsEndpoint, parameters: parameters, encoding: .JSON)
.responseJSON { response in
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print(response.result.error!)
return
}
}
print("func'd")
}
Run Code Online (Sandbox Code Playgroud)
这不是Alamofire
错误或迅速错误,服务器返回的响应不是JSON格式。您可以打印出响应数据并检查其中有什么问题。
尝试使用此代码打印出我们的服务器数据,以轻松识别错误并解决。
Alamofire.request("Your url").responseJSON(completionHandler: { (response) in
switch response.result {
case .success(let value):
break
case .failure(let error):
print("\n\n===========Error===========")
print("Error Code: \(error._code)")
print("Error Messsage: \(error.localizedDescription)")
if let data = response.data, let str = String(data: data, encoding: String.Encoding.utf8){
print("Server Error: " + str)
}
debugPrint(error as Any)
print("===========================\n\n")
}
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11297 次 |
最近记录: |