swift error“Domain = NSCocoaErrorDomain Code = 3840”字符1周围的值无效。“

时间:2016-05-03 06:20:53

标签: swift post error-handling alamofire

我无法在Swift Alamofire POST请求中修复此错误消息(登录用户)。

  

'3840'“字符1周围的值无效。”

我已导入基金会, Alamofire SwiftyJson 。没有授权限制(没有Oauth等)。当我更改Post(例如,使用其他参数和值)时,我也会收到相同的错误消息,但保持其余的代码/格式相同。在我的drupal7 REST服务器'定义'上,它将端点列为/ rest / user / login,将参数列为'username'和'password'字符串,如我所用。

我真的很感激任何提示和帮助吗?

  

调用REQUEST时出错   错误域= NSCocoaErrorDomain代码= 3840“字符1周围的值无效” UserInfo = {NSDebugDescription =字符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)
    }

}

谢谢

5 个答案:

答案 0 :(得分:2)

这是一个错误,指出来自服务器的响应是无效的JSON并且包含不可读的字符(例如html)基本上它意味着您的上传不顺利(服务器不接受,因此不显示json响应)或者你没有正确设置json响应,它包含html。您可以通过查看浏览器中的页面并查看来源来检查后者。

希望这有帮助!

Ps:我假设您在真实代码中使用了工作网址吗? ; - )

答案 1 :(得分:1)

在URL端点末尾添加.json已解决错误。即https://www.example.com/rest/user/login.json

答案 2 :(得分:1)

当问题实际上是服务器端django中的导入错误时,我收到此错误。通过在wireshark中跟踪tcp流,我得到了这个:

'ImportError':< type'exception.ImportError'>,

答案 3 :(得分:0)

我相信错误" 3840字符1和#34之间的值无效;也是404 Page Not Found error

含义Alamofire实际上并没有连接到服务器端。

请记住以下指南:

  1. Double或我应该说Triple检查您的URL端点。
  2. 同时检查您是否使用了正确的参数编码。
  3. 我建议您为API请求创建Alamofire路由器。 (https://github.com/Alamofire/Alamofire#crud--authorization

答案 4 :(得分:0)

如果最初运行时现在无法序列化,则可能由于服务器已关闭而发生。当服务器关闭页面(html)会作为响应返回。因此请不要忘记在浏览器中检查您的API Error Page