-2

// Hi, i got response from url but getting error as below in title, please fix the issue.

Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0

    let Url = String(format: "http://www.fgndbdsn.in/index.php?route=api/mywebservices/addition")
    guard let serviceUrl = URL(string: Url) else { return }

    let parameterDictionary = ["first" : "25", "second" : "25"]
    var request = URLRequest(url: serviceUrl)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    guard let httpBody = try? JSONSerialization.data(withJSONObject: parameterDictionary, options: []) else {
        return
    }
    request.httpBody = httpBody

    let session = URLSession.shared
    session.dataTask(with: request) { (data, response, error) in
        if let response = response {
            print(response)
        }
        if let data = data {

            print(data)
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [[String:Any]]
                print(json)
            }catch {
                print(error)
            }
        }
        }.resume()
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Possible duplicate of [Cocoa error 3840 using JSON (iOS)](https://stackoverflow.com/questions/14171111/cocoa-error-3840-using-json-ios) – Pratik Sodha Sep 01 '18 at 10:47
  • by string concatenation i get appropriated data response but i pass dictionary getting this type of error? – user10109375 Sep 01 '18 at 10:50

1 Answers1

0

The response from the server must be valid JSON with a top level container which is an array or dictionary.

Pratik Sodha
  • 3,679
  • 2
  • 19
  • 38