How can I fix "Error Domain = NSCocoaErrorDomain Code = 3840" No value. "UserInfo = {NSDebugDescription = No value.}"

When I run my code, I get this error and I do not know why.

Domain Error = NSCocoaErrorDomain Code = 3840 "No Value". UserInfo = {NSDebugDescription = No value.}

I searched it on the Internet, but I did not find anything.

This is my code:

let myUrl = NSURL(string: "http://foodhelper.club/registerUser.php"); let request = NSMutableURLRequest(URL:myUrl!); request.HTTPMethod = "POST"; let postString = "userEmail=\(userEmail!)&userFirstName=\(userFirstName!)&userLastName=\(userLastName!)&userPassword=\(userPassword!)"; request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding); NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in dispatch_async(dispatch_get_main_queue()) { if error != nil { self.alertMessage(error!.localizedDescription) print("fail") return } do { let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary print ("1") if let parseJSON = json { let userId = parseJSON["userId"] as? String print ("2") 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.navigationController?.popViewControllerAnimated(true) } myAlert.addAction(okAction); self.presentViewController(myAlert, animated: true, completion: nil) } else { let errorMessage = parseJSON["message"] as? String print ("3") if(errorMessage != nil) { self.alertMessage(errorMessage!) } } } } catch{ //email vergleich fehlt, egal ob print(error) print("catched error") let myAlert = UIAlertController(title: "Alert", message: "Registration successful", preferredStyle: UIAlertControllerStyle.Alert); let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){(action) in self.navigationController?.popViewControllerAnimated(true) } myAlert.addAction(okAction); self.presentViewController(myAlert, animated: true, completion: nil) } } }).resume() } 

thanks for the help

+6
source share
1 answer

You need to set the content header value to use JSON.

 request.addValue("application/json", forHTTPHeaderField: "Content-Type") 

Updated the code to Swift 3 and deleted everything not related to the request:

 let myUrl = URL(string: "http://foodhelper.club/registerUser.php"); var request = URLRequest(url:myUrl!); request.httpMethod = "POST"; request.addValue("application/json", forHTTPHeaderField: "Content-Type") let postString = "userEmail=email&userFirstName=firstname&userLastName=lastname&userPassword=password"; request.httpBody = postString.data(using: String.Encoding.utf8); URLSession.shared.dataTask(with: request, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in if error != nil { print("fail") return } do { let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary print ("1") if let parseJSON = json { let userId = parseJSON["userId"] as? String print ("2") if( userId != nil) { } else { let errorMessage = parseJSON["message"] as? String print ("3") } } } catch{ print(error) } }).resume() 
+5
source

All Articles