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.}
Here is my code:
let myUrl = NSURL(string: "http://www.01coders.com/ios/userRegister.php");
let request = NSMutableURLRequest(URL : myUrl!);
request.HTTPMethod = "POST";
let postString = "username=\(username)&password=\(password)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error -> Void in
if (error != nil){
print("error=\(error)")
return
}
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
print("json: \(json)");
let resultValue = json["status"] as! String;
print("Result: \(resultValue)")
var isUserRegistered:Bool = false;
if(resultValue=="Success"){
isUserRegistered = true;
}
var messageTodisplay:String = json["message"] as! String;
if(!isUserRegistered){
messageTodisplay = json["message"] as! String;
}
dispatch_async(dispatch_get_main_queue(), {
let myAlert = UIAlertController(title: "Alert", message: messageTodisplay, preferredStyle: UIAlertControllerStyle.Alert);
let okaction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil);
myAlert.addAction(okaction);
self.presentViewController(myAlert, animated:true, completion:nil);
})
}catch let error as NSError{
print(error)
}
}
task.resume();