0

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

This is my code:

let correo = txtCorreo.text
let contrasena = txtContra.text

let postString = "correo=\(correo!)&contrasena=\(contrasena!)"
print(postString)

let url = URL(string: "http://localhost:8080/swiftdb/logear.php")!
var request = URLRequest(url: url)

request.httpMethod = "POST"//tipo de envio -> metodo post
request.httpBody = postString.data(using: .utf8)// concatenar mis variables con codificacion utf8

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let data = data else { //si existe un error se termina la funcion
        self.txtError.text = "error del servidor";

        print("solicitud fallida \(error)")//manejamos el error
        return //rompemos el bloque de codigo
    }

    do { //creamos nuestro objeto json
        print("recibimos respuesta")

        if let json = try JSONSerialization.jsonObject(with: data) as? [String: String] {
            DispatchQueue.main.async {//proceso principal
                let mensaje = json["mensaje"]//constante

                let error = Int(json["error"]!)//constante
                print(error!)

                if(error == 1){//todo correcto
                    let sb : UIStoryboard = UIStoryboard(name: "Main",bundle:nil)
                    let nv = sb.instantiateViewController(withIdentifier: "OP") as! OpcionesController
                }else if(error == 2){//datos denegados
                }else if(error == 3){//no existe
                }

                self.txtError.text = mensaje;
            }
        }
    } catch let parseError {//manejamos el error
        print("error al parsear: \(parseError)")
        self.txtError.text = "error del servidor (json)";

        let responseString = String(data: data, encoding: .utf8)
        print("respuesta : \(responseString)")
    }
}
task.resume()

I've tried to add options .allowfragments but still it doesn't work. I also checked in my PHP what is sending from my Swift project to the PHP file and the data is correct.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Netox
  • 45
  • 2
  • 10
  • There's a free app for MacOS called Rested. You might want to use that to get an example of your JSON string and post it with your question. – Adrian Mar 03 '18 at 15:55
  • Which line is printing the error? – rmaddy Mar 03 '18 at 16:02
  • @rmaddy it doesn't display in the code or console in which line is printing. – Netox Mar 03 '18 at 16:05
  • Where is the json sample? – hasan Mar 03 '18 at 16:14
  • Optional("") it displays that – Netox Mar 03 '18 at 16:22
  • It would appear that your request failed for some reason. You should check `error` of the `dataTask` and make sure it's `nil` (tho admittedly, it's likely `nil` if `data` wasn't). I'd also suggest printing the `response` of the `dataTask`, too, and see if it reports a status code other than 2xx. But without seeing this `statusCode` and the `error` of the `dataTask`, we're just guessing. – Rob Mar 03 '18 at 17:22
  • By the way, I notice that you're percent encoding neither the `correo` nor the `contrasena`. If either of those had restricted characters, the request would fail (and you'd end up with a non-200 status code). See https://stackoverflow.com/a/28027627/1271826 for example of how to percent encode these parameters. Or consider using Alamofire, which gets you out of the weeds of preparing requests. – Rob Mar 03 '18 at 17:30

0 Answers0