0

试图将数据发送到服务器,但每次我运行我的代码时,都会在数据库中添加一个空行,并且 xcode 中会出现错误:“字符 1 周围的值无效。” UserInfo={NSDebugDescription=字符 1 周围的值无效。}

 @IBAction func submitButton(_ sender: Any) {
    let parameters = ["paramname": "abc", 
               "paramemail": "hammadnaeem680@gmail.com",
               "parammobile": "1234567890", 
               "paramroomno": "402",
               "paramtime": "9",
               "paramsno": "137865",
               "paramcomplaint": "ios appp check",
               "paramblock": "CSE", 
                "paramtype": "server"] as Dictionary< String, String>
 guard let url=URL(string:"http://uietcloud.puchd.ac.in/mail/submit.php") 
   else {
            return
        }
        let session = URLSession.shared

        var request = URLRequest(url: url)
        request.httpMethod = "POST"
      request.setValue("Value", forHTTPHeaderField: "Key")

        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {
            return}

         request.httpBody = httpBody

        request.addValue("application/json", forHTTPHeaderField: "Accept")
        session.dataTask(with: request) { (data, response, error) in    
         if let data = data
            {
                do
                {
                    let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
                    print(json)
                    print("not an error")
                } 
                catch{
                    print(error)
                    print("this is an error")
                      }

             }
        }
        .resume()
        }
4

1 回答 1

0

序列化为 JSON 时尝试关闭 prettyPrinted 选项。您的服务器可能希望它丑陋。:)

于 2017-08-13T06:04:52.550 回答