Mro Mro - 6 months ago 46
SQL Question

JSON option to allow fragments not set


I can see this question has been asked before but can't find that anyone has accepted an answer anywhere. My error is.
NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.


let myUrl = NSURL(string: "http://www.example.com/xxx.php")
let request = NSMutableURLRequest(URL:myUrl!);
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST";

let postString = "email=\(useremail)&pass=\(userpassword)&username=\(username)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = session.dataTaskWithRequest(request) { (data, response, error) in
// if error
if error != nil {
print("error=\(error)")
return
}

var json: NSDictionary? = nil
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
} catch {
print("error=\(error)")
}
if let parseJSON = json {

let resultvalue = parseJSON["status"] as? String
print("result: \(resultvalue)")


var isuserregistered:Bool = false;
if(resultvalue=="Success") { isuserregistered = true; }

var messagetodisplay:String = parseJSON["message"] as! String!;
if(!isuserregistered)
{
messagetodisplay = parseJSON["message"] as! String!;

}}



I am trying to send email, pass, username
and successfully presenting my alert and changing view controller. but I am yet to hit database. It's PHP and MYSQL via url. help me hit my database :)

Answer

try changing this line

json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

to this

json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSDictionary
Comments