Error Domain = NSCocoaErrorDomain Code = 3840“字符1周围的值无效。”UserInfo = {NSDebugDescription =字符周围的值无效1. swift 4

问题描述 投票:0回答:1

我通过邮递员得到了api的回复

{
"status": "1",
"error": false,
"message": "Your order has been placed successfully"
}

我打电话给api并通过需要调用api的params。代码给出错误“字符1周围的值无效”。

    let urlsContainer = UrlsContainer()
    let url = URL(string: urlsContainer.allotRunnerAPI)
    let session = URLSession.shared
    var request = URLRequest(url: url!)
    let postString = "user_id=\(user_id)&pincode=\(pincode)&select_address=\(select_address)&store_id=\(store_id)"
    request.httpMethod = "POST"
    request.httpBody = postString.data(using: .utf8)
    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
    guard error == nil else {
    return
    }
    guard let data = data else {
    return
    }

代码应该执行do代码块

do {
    let parsedData = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: AnyObject]
    print(parsedData)
    }

但现在由于代码中的一些问题,它正在执行catch代码块

 catch let error {
    print(error)
    }
   })
  task.resume()

我无法在我的代码中找到解决错误的问题

ios swift api nsjsonserialization
1个回答
0
投票

使用[String:String]将数据发送到api

let params = ["user_id":user_id,
"pincode":pincode,"select_address":select_address,
"store_id":store_id]
let data = JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)

将此数据发布为您的身体

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