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

问题描述 投票:-2回答:1

//嗨,我收到了来自网址的回复,但收到的错误如下所示,请解决问题。

错误域= 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()
ios iphone swift web-services swift3
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.