Я новичок в программировании Swift 4.Это мой первый iOS-проект.У меня проблемы с отправкой данных на сервер в формате JSON!
Сервер получает все текстовые поля параметров пустыми.
let pram = ["departement": departmentTextField.text, "fname": firstNameTextField.text, "lname": lastNameTextField.text, "email": emailTextField.text, "mbl": mobileNumberTextField.text, "address": residentialTextField.text,"subrub": suburbTextField.text,"dob": chooseDateofBTextField.text,"gender": genderTextField.text, "cardno": medicareCardNoTextField.text, "edate": medicareExpiryTextField.text, "source": howDidText.text, "condition": conditionTextField.text, "appdate": aptDOPTextField.text]
let url = URL(string: "//myAPI")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = pram
request.httpBody = (postString as AnyObject).data
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
let alert = UIAlertController(title: "Connection Lost ", message: "Please Try Again Later", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
let alert = UIAlertController(title: "Your Appointement has been placed", message: "", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
task.resume()