我一直试图通过php将某些数据插入mysql。但是,我得到了一个编码为3840的错误。下面是我正在处理的代码:
@IBAction func btnVerify(_ sender: Any) {
let myUrl = URL(string: "http://kumbhkaran.co.in/ios_otp_check/verifyOTP.php");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";
let postString = "category=\(Category)&subcategory=\(SubCategory)&vendorname=\(ShopName)&managername=\(ManagerName)&managercontact=\(ManagerMobile)&mobile=\(UserName)&landline=\(Landline)&email=\(Email)&website=\(Website)&city=\(City)&address=\(Address)&area=\(Area)&pincode=\(Pincode)&rentowned=\(ShopStatus)&homedelivary=\(HomeDelivery)&pwd=\(Password)&marketing_ref=\(MarketingRef)&Working_Start_time=\(StartTime)&Working_End_time=\(EndTime)"
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async
{
//spinningActivity!.hide(true)
if error != nil {
self.displayAlertMessage(messageToDisplay: error!.localizedDescription)
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let userId = parseJSON["message"] as? String
if( userId != nil)
{
let myAlert = UIAlertController(title: "Alert", message: "Registration successful", preferredStyle: UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){(action) in
self.dismiss(animated: true, completion: nil)
}
myAlert.addAction(okAction);
self.present(myAlert, animated: true, completion: nil)
} else {
let errorMessage = parseJSON["message"] as? String
if(errorMessage != nil)
{
self.displayAlertMessage(messageToDisplay: errorMessage!)
}
}
}
} catch{
print(error)
}
}
}
task.resume()
}然而,在所有这些代码之后,我得到了如下所述的错误:
错误Domain=NSCocoaErrorDomain Code=3840 "JSON文本没有从数组或对象和选项开始,以允许不设置片段。UserInfo={NSDebugDescription=JSON文本没有以数组或对象和选项开头,以允许不设置片段。
发布于 2017-06-23 12:45:07
试试这个:
将选项值设置为allowFragments而不是mutableContainers。可能json响应没有正确地形成。
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionaryhttps://stackoverflow.com/questions/44721623
复制相似问题