【问题标题】:Error (Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}错误(错误域=NSCocoaErrorDomain 代码=3840“字符 0 周围的值无效。” UserInfo={NSDebugDescription=字符 0 周围的值无效。}
【发布时间】:2016-09-19 23:12:25
【问题描述】:

我收到此错误为 JSON result.error。虽然我的 JSON 是有效的,但请在 JSON validator online 上检查它。

这是我的 JSON 请求代码。

Alamofire.request(.POST, url, parameters: parameters, encoding:.JSON)
                .responseJSON { (request, response, result) in
                    hud.hide(true)
                    // Set flag to disale poor internet connection alert
                    weakInternet = false
                    print(result.error)
                    if (result.value != nil) {
                        print("API Response: \(result.value!)")
                        // Pass the response JSON to the completion block
                        completion(json: result.value!)
                    } else {
                        // Response JSON is NULL
                    }
            }

当我使用特定的请求参数访问相同的服务时,我会收到此响应。

{"error":"success","post_data":{"first_name":"hd","last_name":"df","email":"hiiaaaaaaa@dnsa.coma","password":"himanshu","confirm_password":"himanshu","companies":["Big Rattle Technologies_Fg_yes"],"institutes":[""]},"msg":"success","data":{"_id":"5742ae1564b35e37369f0838","first_name":"hd","last_name":"df","email":"hiiaaaaaaa@dnsa.coma","profile_pic":"","loc":[0,0],"locs":{"type":"Point","coordinates":[0,0]},"institutes":[],"companies":[{"comapny_id":"555b2d0a678e79ed510041ce","group_id":"556c2434678e79a1820041dd","name":"Big Rattle Technologies","designation":"Fg","is_current":"yes"}],"device_token":"","user_group":"site_user","is_verified":0,"is_disclose":1,"is_discover":1,"wallNotification":1,"messageNotification":1,"matchNotification":1,"verificationSent":0,"status":1,"mobile":"","linkedin_id":"","facebook_id":"","os":"","qblox_id":12957726,"updated_at":"2016-05-23 07:15:33","created_at":"2016-05-23 07:15:33","current_company":"Big Rattle Technologies"}}

有人知道我的问题是什么吗?

【问题讨论】:

  • 试试.responseString { response in print("Response String: \(response.result.value)") }而不是responseJSON看看结果是什么样子的。
  • Invalid value around character 0 您的响应不是有效的 JSON。
  • 但是,我在在线 json 查看器上检查了服务的邮递员响应,它显示响应是有效的。
  • 我也遇到了同样的错误,因为我的响应是无效的 JSON 格式。它的 DOCTYPE 是 html。

标签: ios json swift alamofire


【解决方案1】:

我的网络服务有问题。他们以“文本/HTML”格式而不是 HTML 格式给我回复。当我在调试器上打印我的响应时,我得到了:

"Content-Type" = "text/html; charset=UTF-8";

现在,我更新了我的网络服务,一切正常。

【讨论】:

  • 你在哪里加入了这个?
  • 在我的网络服务响应中。
  • 你如何从你的应用程序 web 服务代码或后端 php web 服务更改中解决你的问题
【解决方案2】:

我上次遇到同样的错误,因为会有问题是 Web 服务以数组形式返回我的响应,我正在尝试将其转换为字典并提取其值。

检查您的网络服务响应。

【讨论】:

    【解决方案3】:

    斯威夫特 5,斯威夫特 4

    var headers = HTTPHeaders()
        headers = [
            "Content-Type" :"text/html; charset=UTF-8",
            //"Content-Type": "application/json",
            //"Content-Type": "application/x-www-form-urlencoded",
            //"Accept": "application/json",
            "Accept": "multipart/form-data"
        ]
    

    【讨论】:

      【解决方案4】:

      我在使用 错误 Firebase 服务器密钥发送远程推送通知时收到此错误。

      转到Firebase > Project Overview > CogIcon > Project Settings > Cloud Messaging > Server Key

      guard let url = URL(string: "https://fcm.googleapis.com/fcm/send") else { return }    
      
      var request = URLRequest(url: url)
      request.httpMethod = "POST"
      request.httpBody = try?
      
      let paramDict = [String:Any]() // this has values
      JSONSerialization.data(withJSONObject: paramDict, options: [])
      request.setValue("application/json", forHTTPHeaderField: "Content-Type")
      
      let serverKey = "firebase server key" // *** SERVER KEY USED HERE ***
      request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
      
      let task = URLSession.shared.dataTask(with: request)  { (data, response, error) in
      
          do {
              if let jsonData = data {
                          
                  if let jsonDataDict = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] {
                      print("jsonData Successfully Sent data: \n\(jsonDataDict))")
                  }
              }
          } catch let err as NSError {
              print("jsonData Error: \(err.debugDescription)")
          }
      }
      task.resume()
      

      【讨论】:

        【解决方案5】:

        确保从服务器关闭防火墙

        【讨论】:

          猜你喜欢
          • 2018-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-24
          • 2018-06-17
          • 2014-11-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多