Click here to Skip to main content
15,843,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);  
?>  





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


What I have tried:

parsing JSON is valid but my i guess my error is PHP i don't know what to do
Posted
Comments
Richard MacCutchan 18-Oct-18 11:32am    
The issue is clearly in the server. You need to find out where this data is being sent from.
Member 14024497 18-Oct-18 11:38am    
can you be more specific?

the data is being sent to a URL which is linked to my PHP code
i'm consulting the data from xcode using json
let myURL = URL(string: "hehe")
Richard MacCutchan 18-Oct-18 12:22pm    
Where is the data string being created? That must be the place to look.
Member 14024497 18-Oct-18 12:09pm    
yeah php code already fix

but still getting same error it's so frustrating can't find the answer any place

<?php
session_start();
error_reporting(0);
echo '<meta charset="UTF-8">';
$host_db = "hehe";
$user_db = ":D";
$pass_db = "D:";
$db_name = "=DDD";
$conexion = new mysqli($host_db, $user_db, $pass_db, $db_name);
if ($conexion->connect_error) {
die("La conexion falló: " . $conexion->connect_error);
}

$sql = "SELECT * FROM Usuarios where Correo = '".$_POST['Email']."' and Password = '".$_POST['Password']."'";
$result = $conexion->query($sql);
if ($result->num_rows > 0) {
$row = mysqli_fetch_array($result);

$response['Email'] = $row['Correo'];
$response['Password'] = $row['Password'];
}
else
{
$response['Email'] = 'Error';
$response['Password'] = 'Error';
}
echo json_encode($response);
mysqli_close($conexion);
?>
Richard Deeming 18-Oct-18 12:20pm    
echo '<meta charset="UTF-8">';

Really? You can't see where in that code the <meta charset="UTF-8"> is coming from? :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900