I received an error when I clicked to the button delete in my app . Iwant to delete data from database MySql + PHP :
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.}
ViewController–>my code
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var id: UITextField!
//Send user data to server side
@IBAction func deleteData(_ sender: AnyObject) {
//Send user data to server side
let myUrl = NSURL(string: "http://192.168.1.25/ios/delete.php");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
let postString = "id=\(id)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
{
data, response, error in
if error != nil {
print("error=\(error)")
return
}
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
if let parseJSON=json{
var resultValue = parseJSON["status"] as! String
print("result: \(resultValue)")
var isUserRegistered:Bool = false;
if(resultValue=="Success") {isUserRegistered = true; }
var messageToDisplay:String = parseJSON["status"] as! String
if(!isUserRegistered)
{
messageToDisplay = parseJSON["message"] as! String;
}
//self.store.storeName = self.lblName.text!
//store?.sName = txtName.text!
self.store.storeName = self.lblName.text!
dispatch_async(dispatch_get_main_queue(), {
// Display alert message with confirmation.
var myAlert = UIAlertController(title:"Alert", message:messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default) {action in
self.dismissViewControllerAnimated(true, completion:nil);
}
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated: true, completion:nil);
});
}
}catch{print(error)}
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}