2017-08-12 100 views
0

試圖發送數據到服務器,但每次我運行我的代碼時,一個空行被添加到數據庫中,並在xcode中出現錯誤:「字符1周圍的值無效。 UserInfo = {NSDebugDescription =字符1周圍的值無效}}字符無效值1

@IBAction func submitButton(_ sender: Any) { 
    let parameters = ["paramname": "abc", 
       "paramemail": "[email protected]", 
       "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() 
     } 

回答

0

嘗試在序列化爲JSON時關閉prettyPrinted選項。你的服務器可能希望它很醜陋。 :)

+0

仍然無法正常工作,我更新了行:警衛讓httpBody =嘗試? JSONSerialization.data(withJSONObject:parameters,options:[])else {{{0} {0}} {返回} – hammad119