xcode alamofire.error Code=-6006 "JSON 无法序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35374798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
alamofire.error Code=-6006 "JSON could not be serialized
提问by James H
Been working on this for a bit with no success. I have a function that goes to a UIButton
solely to perform alamofire calls to my rails api which uses all JSON.
一直在研究这个,但没有成功。我有一个函数,该函数UIButton
仅用于对使用所有 JSON 的 rails api 执行 alamofire 调用。
I'm using Swift 2, Alamofire 3, XCode 7 & Rails 4 for my api which is deployed to Heroku
我正在使用 Swift 2、Alamofire 3、XCode 7 和 Rails 4 作为我部署到 Heroku 的 api
I keep getting this error when I fire off the function :
当我关闭该功能时,我不断收到此错误:
alamofire.error Code=-6006 "JSON could not be serialized. Input data was nil or zero length.
alamofire.error Code=-6006 "JSON 无法序列化。输入数据为零或零长度。
Here is my code :
这是我的代码:
@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")
}
采纳答案by AshvinGudaliya
This is not Alamofire
or swift error, The response returned by the server is not in the JSON format. you can print out response data and check what is wrong in this.
这不是Alamofire
或 swift 错误,服务器返回的响应不是 JSON 格式。您可以打印出响应数据并检查其中有什么问题。
try this code to print out our server data to easily identifying to error and resolve this.
尝试使用此代码打印出我们的服务器数据,以便轻松识别错误并解决此问题。
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")
}
})
回答by TBXark VFanx
The response returned by the server is not in the JSON format. You can use the tool to test the request first.
服务器返回的响应不是JSON格式。您可以先使用该工具测试请求。
Print out of the error code is not a HTTP error code, because of the failure to resolve JSON
打印出来的错误码不是HTTP错误码,因为解析JSON失败
回答by Yogendra Singh
You need to check the mimeType it will be "text/plain" instead of "application/json". That's why JSONSerialization class not able to parse the data.
您需要检查 mimeType 它将是“text/plain”而不是“application/json”。这就是 JSONSerialization 类无法解析数据的原因。