简体   繁体   中英

Error Domain=NSCocoaErrorDomain Code=3840 JSONObject

I am trying to consume a rest web service with switf 4 from xcode, this is my code:

func login_crm(user:String, passwd:String) {


    let url = URL(string: "https://intranet.jytcolombia.com/ws/ws_ios/iosws_login.php?par1=\(user)&par2=\(passwd)")
    let request = URLRequest(url: url!)
    let task = URLSession.shared.dataTask(with: request){data, response, error in
        guard let data = data else{
            print("Linea 33 \(error)")
            return
        }
        do{
            print("Recibimos respuesta")
            if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: String] {
                DispatchQueue.main.async {
                    let result = json["login"]

                    if(result == "true"){

                        let result = json["login"]

                        if(result == "true"){



                        } else {
                            let alert = UIAlertController(title: "Inicio de Sesion", message: "Usuario o contraseña invalidos", preferredStyle: .alert)
                            alert.addAction(UIAlertAction(title: "OK", style: .default))
                            self.present(alert, animated: true, completion: nil)
                        }

                    } else {
                        let alert = UIAlertController(title: "Inicio de Sesion", message: "Usuario o contraseña invalidos", preferredStyle: .alert)
                        alert.addAction(UIAlertAction(title: "OK", style: .default))
                        self.present(alert, animated: true, completion: nil)
                    }

                }
            }

        }catch let parseError{
            print("Linea 50: \(parseError)")
            let responseString = String(data: data, encoding: .utf8)
            print("Linea 52: \(responseString)")
        }
    }
    task.resume()



}

This is my answer in the xcode console:

Linea 50: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.} Linea 52: Optional("|{\"login\":\"true\"}")

This is my code web services:

|<?php
    header("Content-type: application/json; charset=utf-8");


$response = array();

if($_SERVER['REQUEST_METHOD'] == 'GET'){
    $user = $_GET['par1'];
    $passwd = sha1($_GET['par2']);

    $consulta = "SELECT * FROM USUARIOS WHERE USUARIOS.usuarios_nombreusuario = '$user' AND USUARIOS.usuarios_password = '$passwd' LIMIT 1";
    $proceso = mysqli_query($conexioni, $consulta) or die(mysqli_error($conexioni)); 
    $cols = mysqli_num_rows($proceso);

    if($cols == 0){
        $response['login'] = "false";
    } else{
        $response['login'] = "true";
    }

    echo json_encode($response);
}
?>

I can see that your response string contains a | character:

"|{\"login\":\"true\"}"

And it's causing the json parsing issue. Please check your server code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM