0
    let parameters = ["lat": latitude, "lng": longitude]
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-type")
    guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, 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 httpResponse = response as? HTTPURLResponse {
            print(httpResponse.statusCode)
        }
        
        if let data = data {
            do {
                let jsonDecoder = JSONDecoder()
                
                var parsedJSON = try jsonDecoder.decode([UserCoords].self, from: data)
                parsedJSON.append(Vac_Pass.UserCoords(lat: latitude, lng: longitude))
                let wer = try? JSONEncoder().encode(parsedJSON)
                let json = try JSONSerialization.jsonObject(with: wer!, options: [])
                print(json)
                
                
                print(json)
            } catch {
                print(error)
            }
        }
    }.resume()

由于http post响应显示“200”,这表明请求成功但没有成功上传新数据。如果数据确实被上传,它会打印出“201”。我认为我的本地主机网络服务器可能有问题。

localhost 本质上是一个 JSON 文件,其中包含如下坐标:

     [
      {"lat" : 38.8976, "lng" : -77.0369 } ,
      {"lat" : 38.8980, "lng" : -77.0363 } ,
      {"lat" : 38.8990, "lng" : -77.0367 } 
     ]

谢谢 - 所有输入都表示赞赏:)。

4

0 回答 0