Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

xcode - alamofire.error Code=-6006 "JSON could not be serialized

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.

I'm using Swift 2, Alamofire 3, XCode 7 & Rails 4 for my api which is deployed to Heroku

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.

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")

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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("

===========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("===========================

")
    }

})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...