我无法修复我的 Swift Alamofire POST 请求(登录用户)中的此错误消息。
'3840' "Invalid value around character 1."
我已经导入了 Foundation、Alamofire、SwiftyJson。没有授权限制(没有 Oauth 等)。当我更改 Post(例如,使用其他参数和值更改为另一个端点)但保持其余代码/格式相同时,我也会收到相同的错误消息。在我的 drupal7 REST 服务器“定义”中,它将端点列为/rest/user/login,并将参数列为我使用的“用户名”和“密码”字符串。
我真的很感激任何提示和帮助?
error calling REQUEST Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 1." UserInfo={NSDebugDescription=Invalid value around character 1.}
这是我的代码
@IBAction func loginButtonTapped(sender: AnyObject) {
//using Alamofire
let dataEndpoint: String = "https://www.example.com/rest/user/login"
let newData = ["username":"Mickey", "password":"123"]
Alamofire.request(.POST, dataEndpoint, parameters: newData, encoding: .JSON)
.responseJSON { response in
guard response.result.error == nil else {
// got an error in posting the data, need to handle it
print("error calling REQUEST")
print(response.result.error!)
return
}
guard let value = response.result.value else {
print("no result data when calling request")
return
}
let data = JSON(value)
print("The result is: " + data.description)
}
}
谢谢
最佳答案
这是一个错误,表明来自服务器的响应不是有效的 JSON 并且包含不可读的字符(例如 html)基本上这意味着您的上传不顺利(服务器不接受因此不显示 json 响应)或者您没有正确设置 json 响应并且它包含 html。您可以通过在浏览器中查看页面并检查源来检查后者。
希望这对您有所帮助!
Ps:我假设您在实际代码中使用的是有效 URL,对吧? ;-)
关于swift 错误 "Domain=NSCocoaErrorDomain Code=3840 "字符 1 周围的值无效。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36997007/