如何解决问题?我在体内给出了描述

时间:2018-09-01 10:45:02

标签: ios iphone swift web-services swift3

//嗨,我收到了来自url的响应,但标题中出现以下错误,请解决此问题。

错误域= NSCocoaErrorDomain代码= 3840“字符0周围的值无效。” UserInfo = {NSDebugDescription =字符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()

1 个答案:

答案 0 :(得分:0)

服务器的响应必须是有效的JSON,并带有一个顶级容器,该容器是数组或字典。