HTTP.Body参数错误

时间:2015-11-04 13:43:27

标签: ios xcode swift

我有这个代码,我需要通过BODY传递参数,但是我得到一个“罕见”的错误。有谁能够帮我? THX !!

这是错误

  
    

可选(错误域= NSURLErrorDomain代码= -1017“无法解析响应”UserInfo = {NSUnderlyingError = 0x7fda33f4cd80 {错误域= kCFErrorDomainCFNetwork代码= -1017“(null)”UserInfo = {_ kCFStreamErrorCodeKey = -1,_kCFStreamErrorDomainKey = 4} },NSErrorFailingURLStringKey = http:... / users.php,NSErrorFailingURLKey = http:... / users.php,_kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -1,NSLocalizedDescription =无法解析响应})

  
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

//
// The problem is the following 4 lines, if I tell, there is no error, but of course, I can not register a user
//

do {

    request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(bodyParams, options: [])

} catch {

    IsRegistered(isRegistered: false, messageString: "Error al parsear los datos.")

}


        NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in

            if (error != nil) {

               // ERROR HERE
               // ERROR HERE
               // ERROR HERE
               print(error)

            } else {

               ...

            }
}

bodyParams是一个词典

我看到“所有人”使用此方法按身体传递参数,但我不适合我:(

编辑

var bodyParams: Dictionary<String, String> = Dictionary()
bodyParams["firstName"] = currentUser.getFirstName()
bodyParams["lastName"] = currentUser.getLastName()
bodyParams["gender"] = currentUser.getGender()
bodyParams["birthday"] = currentUser.getBirthday()

0 个答案:

没有答案