SyntaxError: JSON.parse: unexpected end of data at line 1 column

Résolu
gretin Messages postés 6 Date d'inscription   Statut Membre Dernière intervention   -  
gretin Messages postés 6 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,

Je suis en train de faire une simple requête Ajax avec jQuery avec une réponse du server au format json. Cela fonctionne bien sur chrome mais cette erreur apparaît sur Firefox

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
http://code.jquery.com/jquery-2.1.4.min.js
Line 4


voici le code js
$("form").submit(function(e){
   e.preventDefault();
   if(exampleInputFile.val() == "")
   {
      return false
   }
   else
   {
   $.ajax({
            url: "upload.php",
            type: "POST",
            data:  new FormData(this),
            contentType: false,
            cache: false,
            processData:false,
            dataType:"json",
            success: function(data)
            {
               console.log(data);
            },
            error: function( data, status, error ) {
                console.log(data);
                console.log(status);
                console.log(error);
            }
        });
}


et PHP

<?php
if(isset($_POST['exampleInputSubmit']))
    {
        if(!empty($_POST['exampleInputText']))
        {
            $nameFile = htmlspecialchars(trim($_POST['exampleInputText']));
            if (preg_match('/[^A-Za-z0-9_\-]/', $nameFile))
            {
                $return["error"] = "test du format nom";
            }
        else
        {
            $return["valide"] = "pas de nom";
        }

        if(isset($return))
        {
            echo json_encode($return);
        }
        else
        {
            $return["valide"] = "Bien";
            echo json_encode($return);
        }
    }
?>


Je précise que je souhaites par la suite faire un Upload d'image.
Je remercie par avance ceux qui veulent bien m'aider.

3 réponses

jordane45 Messages postés 38480 Date d'inscription   Statut Modérateur Dernière intervention   4 746
 
Bonjour,

Déjà.. ton script PHP.. tu peux l'écrire ainsi :
<?php

$exampleInputSubmit = isset($_POST['exampleInputSubmit'])?$_POST['exampleInputSubmit']:NULL;
$exampleInputText = isset($_POST['exampleInputText']) && !empty($_POST['exampleInputText'])?trim($_POST['exampleInputText']):NULL;

$result=array();
$return["valide"] = "Bien";

 if($exampleInputSubmit){
  if($exampleInputText){
   $nameFile = htmlspecialchars($exampleInputText);
   if (preg_match('/[^A-Za-z0-9_\-]/', $nameFile)){
     $return["error"] = "test du format nom";
   } else {
    $return["valide"] = "pas de nom";
   }
  }
}
echo json_encode($return);
?>


Cordialement,
Jordane
1
jordane45 Messages postés 38480 Date d'inscription   Statut Modérateur Dernière intervention   4 746
 
Voir même ... sans le exempleInputSubmit .. qui me semble superflu.
<?php

$exampleInputText = isset($_POST['exampleInputText']) && !empty($_POST['exampleInputText'])?trim($_POST['exampleInputText']):NULL;

$result=array();
$return["valide"] = "Bien";

if($exampleInputText){
 $nameFile = htmlspecialchars($exampleInputText);
 if (preg_match('/[^A-Za-z0-9_\-]/', $nameFile)){
   $return["error"] = "test du format nom";
 } else {
  $return["valide"] = "pas de nom";
 }
}

echo json_encode($return);
?>

0