0

I have been trying to insert certain data into mysql through php. But however i am getting an error coded 3840.Below is the code i am working on:-

 @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()
}

However after all this code i get an error stated as below:-

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set

himesh
  • 43
  • 1
  • 9

1 Answers1

0

Try this :

Set option value to allowFragments instead of mutableContainers. Probably the json response is not properly formed.

  let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
Fares Benhamouda
  • 589
  • 8
  • 21
  • this worked ok ! but it still gave me an error stating "Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0." Can you please help with this – himesh Jun 23 '17 at 12:54
  • could you provide some example response from the server , or better provide us with some testing link. Then I could definitly help better. – Fares Benhamouda Jun 23 '17 at 14:05
  • In my case your link return "Array" string which is obviously not a valid json. and that's why you get that error message. – Fares Benhamouda Jun 23 '17 at 14:21