Forum Moderators: open

Message Too Old, No Replies

How to solve SyntaxError: JSON.parse: unexpected end of data at line 1

JSON data from PHP response

         

dbarasuk

3:44 pm on Jul 30, 2015 (gmt 0)

10+ Year Member



Hi Friends,
I need your help for my project. I have passed a long time trying to figure out the origin of the error mentionned in the title with no luck.

In fact, my supervisor asked to create a webservice to validate receipt numbers manually typed in a form of an application. We have an application that delivers receipts when our customers have paid. Another application wants to use these receipts numbers to complete their business.

For that I have developped a webservice server using the nusoap PHP class and of course a client for it.

The webservice client is called by AJAX after completing the receipt number field. When the mouse leaves that field, the client PHP is called using AJAX. The client on its turn calls the webservice server which returns an array of when the receipt was paid and the related amount.

When everything is OK, two other fields on the form (The receipt amount and the payment date) are automatically field in by AJAX.

Now the problem is that on my local machine everything works Fine. When I move the same files on a RedHat server, I get the error in FireFox console: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data"

The HTML file is as follows:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link rel="stylesheet" type="text/css" href="rcms-with-bootstrap/bootstrap/css/bootstrap.min.css" />


<!--<link rel="stylesheet" type="text/css" href="rcms/bootstrap3/css/bootstrap.min.css">
<script type="text/javascript" src="rcms/jQuery/prod/jQuery.min.js"></script>
<script type="text/javascript" src="rcms/bootstrap3/js/bootstrap.min.js"></script>-->
</head>
<body style="padding: 0; margin: 0 auto; width: 960px; font-size: 100%;">
<h4>Veuillez saisir le numéro de la quittance </h4>
<div class="table-responsive" style="width: 60%;">
<form action="" method="post">

<table class="table table-striped table-bordered table-condensed">
<tr>
<th class="info col-md-4">Libellé</th>
<th class="info col-md-8" >---</th>
</tr>
<tr>
<td class="col-md-4">Numéro de la quittance</td>
<td class="col-md-8"><input type="text" name="num" id="num"/></td>
</tr>
<tr>
<td class="col-md-4">Montant</td>
<td class="col-md-8"><input type="text" name="montant" id="montant" /></td>
</tr>
<tr>
<td class="col-md-4">Date de paiement</td>
<td class="col-md-8"><input type="text" name="date de paiement" id="date_paie" /></td>
</tr>
</table>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="rcms-with-bootstrap/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$('#num').blur(function(){
var num = $(this).val();
if(num.length >= 1){
// Lancer une requête vers le script client du webservice
var request = $.get('getquittanceclient.php?num=' + num);
request.success(function (response, status){
var montant = response.montant;
var datepaie = response.date_paie;
$('#montant').val(montant);
$('#date_paie').val(datepaie);
if(montant == 'Inexistant' && datepaie == 'Inexistant'){
$('#montant').css({backgroundColor:'red', color: 'white'});
$('#date_paie').css({backgroundColor:'red', color: 'white'});
}else if(montant == 'Quittance Must be' && datepaie == 'A Number'){
$('#montant').css({backgroundColor:'red', color: 'white'});
$('#date_paie').css({backgroundColor:'red', color: 'white'});
}else{
$('#montant').css({backgroundColor:'white', color: 'black'});
$('#date_paie').css({backgroundColor:'white', color: 'black'});
}
console.log(response );
}).error(function (jqXHR, textStatus, errorThrown){
console.log('Type d\'erreur: ' + textStatus + '\nMessage d\'erreur: ' + errorThrown);
});
}

});
});
</script>
</body>
</html>


The PHP webservice server is as follows:
<?php
include_once('nusoap/lib/nusoap.php');

function getQuittance($num){
if(stripos($num, '0') === 0){
return array('Quittance Must be', 'A Number');
}
$conn = mysqli_connect('localhost','valentin', 'valentin', 'colltest')
or die('Erreur de connexion à la Base de données');
if ($conn) {
// Check if quittance exists
$result = mysqli_query($conn, "SELECT COUNT(*) FROM recette WHERE num= '$num'");
// die("La requête contient une erreur SQL: ".mysqli_error($conn));
if(!$result){
mysqli_close($conn);
return array('Connexion', 'Error');
}
if($result){
$how_many = mysqli_fetch_array($result);
if($how_many[0] != 1){
return array('Inexistant', 'Inexistant');
}
else{
// Une requête pour récupérer la quittance
$result2 = mysqli_query($conn, "SELECT * FROM recette WHERE num= '$num'");
// die("Erreur: ".mysqli_error($conn));
if(!$result2){
$error = mysqli_error($conn);
mysqli_close($conn);
return array('Error', $error);
}
if($result2){
$donnees = mysqli_fetch_array($result2);
mysqli_close($conn);
return array($donnees['montant'], $donnees['date_paie']);

}
}
}
}
}

// Créer une instance du serveur
$server = new soap_server();
// Créer un fichier WSDL
$server->configureWSDL('valentin', 'urn:valentin');
// Enregistrer le service dans le serveur
$server->register('getQuittance',
array('num' => 'xsd:integer'), array('montant' => 'xsd:string', 'date_paie' => 'xsd:string'));
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA); // Ici on envoie la réponse serveur au client sous forme XML

?>


The client PHP service is as follows:
<?php
header('Content-type: application/json; charset=utf-8');
$num = $_GET['num'];
// Pour éviter des erreurs, changer la valeur de display_errors à display_errors = Off
include_once('nusoap/lib/nusoap.php');

// Créer le wsdl à interroger
$client = new nusoap_client('http://localhost/getquittance.php?wsdl');

// Voir s'il y a erreur dans la création du client
if ($client->getError()) {
echo json_encode($client->getError());
}
// S'il n'y a pas d'erreur de constructeur, appeler la méthode contenu dans le wsdl avec ses params
$result = $client->call('getQuittance', array('num' => $num));
if ($client->fault) {
echo json_encode($result);
}else{
if ($client->getError()) {
echo json_encode($client->getError());
}else{
echo json_encode($result);
}
}


I really don't see what I have made wrong for the application not to work on linux server while it works perfectly on my windows 7 machine.

Can someone tell me what's wrong? I will be very greatful because It's been too long I have been googling with no success.

Thanks in advance

Fotiman

6:51 pm on Jul 30, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't looked too closely, but perhaps changing this:

var request = $.get('getquittanceclient.php?num=' + num);
request.success(function (response, status){

So that the success handler is passed in with the get call:


$.get('getquittanceclient.php?num=' + num, function () {
// sucess
}).fail() {
// fail
};

dbarasuk

9:34 pm on Jul 30, 2015 (gmt 0)

10+ Year Member



First of all, thanks to read my post.
I have tried your option before. They are both the same except that for your suggestion you loose the chance to use the error() methof on the request object. Thus you have to use the fail() method which works diffently than the error() one.

Please, help me, cause I'm in a trouble

incrediBILL

4:51 am on Aug 5, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have you looked at the actual JSON being returned?

I often see differences in the way something works in windows/linux regarding linefeeds, either too many or not enough.

If you don't know how to use the debugging console to look at the JSON data, try putting an alert() in your code to it pops up and shows you the whole AJAX return value. Sometimes just seeing it on the screen answers a whole bunch of questions.

Such as it's blank, has an error in it, etc.

Another way to quickly look at the return values from the server is to manually call the AJAX URL from your browser, then you can look at the data returned in view source