help with PHP + AJAX + JSON

0

Good! I tell you my problem.

I make a request to a php file with AJAX, in this file I bring a long text from my database (mysql) and I save it in a php array, then I pass it to js with json_encode ();

The problem I have is that there are times when the text shows it well, other times it shows nothing, it says empty ... I suspect that is when several special characters come together ... the strange thing is that when I had my page on a local server with xampp this did not happen, the problem started when I uploaded a free hosting. the error that appears to me is "json parse error", search in google, I read many pages and I could not solve the error! I leave the php and ajax caught.

the php one:

    $sql = "SELECT descripcion FROM localidades WHERE id_localidad='$id'";

    $rs = mysql_query($sql);    

    if(mysql_num_rows($rs)>0){
        while($fila=mysql_fetch_array($rs)){


            //son los campos
            $arr = array('descripcion'=>$fila[0],
                        'success'=>true);
        //$arr = array('descripcion'=> str_replace("\r\n", "\n", $fila[8]), 'success'=>true);
        }



    }else{
        $arr = array('success'=>false);
    }

    echo json_encode($arr);

and this is the ajax with the request:

	jQuery.ajax({
              type:"POST",        
              data:{param: id,
              		param2:"descripcion"},
              url: "scripts/buscar_loc2.php",
              dataType:'json',
              success: function(r){
             // 	var desc = eval ('(' + r.descripcion + ')');
              	
					jQuery("#loc_desc").val(r.descripcion);
					
					//alert(desc);
					},error: function (jqXHR, textStatus, errorThrown) {

  					  alert(textStatus);
		}
    
		
			});

Thank you very much and I hope I can solve this problem!

    
asked by matias 20.05.2017 в 02:01
source

2 answers

0

As you were told above, look at what error gives file.php on the network with the browser's developer tools. Also add this header so that the client knows that you will respond in json format and with utf8 coding (by special characters). Good luck and greetings.

 header('Content-Type: application/json; charset=utf-8');
    
answered by 21.05.2017 в 01:44
0

It is very likely that you have an error in your PHP code that is generating a warning (access a nonexistent index of a matrix, for example) that corrupts the format of the JSON sent to the browser.

To try to solve the problem you could try:

<?php
/* Poner esto JUSTO LO PRIMERO de todo tu código PHP */
ob_start();

/* ... Aquí iría el resto de tu código PHP ... */

$sql = "SELECT descripcion FROM localidades WHERE id_localidad='$id'";
$rs = mysql_query($sql);
$arr = array();   
if(mysql_num_rows($rs)>0){
    while($fila=mysql_fetch_array($rs)){
        //son los campos
        $arr = array('descripcion'=>$fila[0],
                    'success'=>true);
    //$arr = array('descripcion'=> str_replace("\r\n", "\n", $fila[8]), 'success'=>true);
    }
}else{
    $arr = array('success'=>false);
}
/* Definimos la cabecera HTTP correcta */
header('Content-Type: application/json; charset=utf-8');
/* Borramos cualquier mensaje de advertencia previo */
ob_end_clean();
/* Ahora enviamos al navegador la información correcta: */
echo json_encode($arr);

With ob_start() I retain and store any output to the browser in a buffer. With ob_end_clean() I discard any previous output (warning messages).

If you want (I recommend it) you can previously obtain its content and save it in a file for further analysis.

    
answered by 22.05.2017 в 09:21