【问题标题】:Error Domain=NSCocoaErrorDomain Code=3840 JSONObject错误域=NSCocoaErrorDomain 代码=3840 JSONObject
【发布时间】:2020-03-14 02:02:08
【问题描述】:

我正在尝试使用来自 xcode 的 switf 4 使用 rest web 服务,这是我的代码:

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()



}

这是我在 xcode 控制台中的回答:

Linea 50:错误域=NSCocoaErrorDomain 代码=3840“无效值 围绕字符 0。” UserInfo={NSDebugDescription=无效值围绕 字符 0.} 第 52 行:可选("|{\"login\":\"true\"}")

这是我的代码网络服务:

|<?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);
}
?>

【问题讨论】:

    标签: php ios json swift xcode


    【解决方案1】:

    我可以看到您的响应字符串包含一个 |人物:

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

    它导致了 json 解析问题。请检查您的服务器代码。

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 2018-04-08
      • 2015-03-11
      • 2016-02-26
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      相关资源
      最近更新 更多