问题描述
我收到此错误:
可选(< meta charset = \UTF-8 \> {\电子邮件\ :\ Error\ \ Password\:\ Error\} \r\\\
\r\\\
\r\\\
)
错误域= NSCocoaErrorDomain代码= 3840JSON文本没有以数组或对象开头,并且选项允许未设置片段。 UserInfo = {NSDebugDescription = JSON文本没有以数组或对象开头,并且选项允许未设置片段。}
我的服务器发送的回复类似于< ; meta charset =UTF-8> {电子邮件:错误,密码:错误}。
绝对不是有效的JSON,因为它开始于`<`
i不知道为什么它不起作用因为我有日志正确使用HTML代码从头开始
我的PHP代码
I'm getting this error:
Optional("<meta charset=\"UTF-8\">{\"Email\":\"Error\",\"Password\":\"Error\"}\r\n\r\n\r\n")
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
my server is sending a response like <meta charset="UTF-8">{"Email":"Error","Password":"Error"}.
It definitely is not a valid JSON, as it starts with `<`
i don't know why it's not working because i had log in correctly with HTML code from scratch
my php code
<?php
// Create connection
$con=mysqli_connect("cantshowthis","hehe","sorry",":c");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Registro'
$sql = "SELECT * FROM Registro";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>
这是我的XCODE代码
this is my XCODE code
@IBAction func iniciarSesion(_ sender: UIButton) {
guard
let emailText = Email.text, !emailText.isEmpty,
let contrasenaText = Contrasena.text, !contrasenaText.isEmpty else
{
displayAlert(title: "Información Faltante", message: "Debes porporcionar un correo y contraseña")
return
}
let myURL = URL(string: "hehe")
var request = URLRequest(url: myURL!) rather than `NSMutableURLRequest`
request.httpMethod = "POST"
let posString = "Email=\(emailText)&Password=\(contrasenaText)"
request.httpBody = posString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
if let error = error {
print("error=\(error)")
return
}
guard let data = data else {
print("Something wrong")
return
}
print(String(data: data, encoding: .utf8) as Any)
do {
let json = try JSONSerialization.jsonObject(with: data)
if let parseJSON = json as? [String: Any]{
guard let resultValue = parseJSON["status"] as? String else {
print("No `status` in result")
return
}
print("message: \(resultValue) ")
if (resultValue == "success") {
UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
UserDefaults.standard.synchronize()
} else {
cases
}
} else {
print("Result JSON is ill-formatted:", json)
}
} catch let err {
print(err)
}
}
task.resume()
}
我尝试了什么:
解析JSON是有效的但我猜我的错误是PHP我不知道该怎么办
What I have tried:
parsing JSON is valid but my i guess my error is PHP i don't know what to do
推荐答案
这篇关于使用JSON从PHP获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!