gpt4 book ai didi

php - 错误域=NSCocoaErrorDomain 代码=3840 JSONObject

转载 作者:行者123 更新时间:2023-11-28 23:25:08 25 4
gpt4 key购买 nike

我正在尝试使用来自 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: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.} Linea 52: Optional("|{\"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);
}
?>

最佳答案

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

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

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

关于php - 错误域=NSCocoaErrorDomain 代码=3840 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58919004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com