JQuery - Cambiar character set... Como se hace?

 
Vista:

Cambiar character set... Como se hace?

Publicado por AJaramillo (2 intervenciones) el 28/02/2012 22:41:34
Hola, como se topan.....

Ando haciendo unas pruebas con este codigo, pero no he logrado hacerlo cancionar como debe ser...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?
session_start();
 
include_once("lee_base.php");
include_once("configuracion/abrirbd.php");
 
if (isset($_POST["accion"])) {
	$ced = $_POST["ced"];
	$nom = trim($_POST["nom"]);
	if (strlen($ced) >0 )
		$sql = "SELECT cedula, nombre FROM maestro WHERE cedula='$ced'";
	else
		$sql = "SELECT cedula, nombre FROM maestro WHERE nombre='$nom'";
	if ($res = lee_todo($sql)) {
		$content = array("data" => $res,
					 	"sql" => $sql
						);
	}
	else {
		$content = array("sql" => $sql,
					 	"msg" => "Error en query"
						);
	}
	armaRespuesta($content);
	die;
}
muestra_forma();
die;
 
function armaRespuesta($content) {
	if ( !function_exists('json_encode') ){
   				function json_encode($content){
					require_once 'JSON.php';
					$json = new Services_JSON;
					return $json->encode($content);
    			}
	}
	echo json_encode($content);
	die;
}
 
function muestra_forma() {
?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=LATIN1" />
		<script language="javascript" type="text/javascript" src="js/jquery.js"></script>
		<script>
			$(document).ready( function () {
				$("input").change( function () {
					datos = $("#prueba").serialize()+"&accion=envia";
					$.ajax({
						data: datos,
						type: "post",
						dataType: "json",
						ContentType: "application/json; charset=iso-8859-1",
						url: "c.php",
						success: function(json) {
console.log(json);
							$.each(json.data, function (clave, valor) {
								$("#nom").val(valor.nombre);
							});
						}
					});
				});
				return false;
			});
		</script>
	</head>
	<body>
		<form action="<?= $PHP_SELF; ?>" name="prueba" id="prueba" method="post">
			<td>Id: <input type="text" id="ced" name="ced" value=""></td>
                      	<td>Nombre: <input type="text" id="nom" name="nom" value=""></td>
		</form>
	</body>
</html>
<?
} ?>


EL lío que tengo es que al hacer una consulta por Id el campo nombre lo retorna en nulo en los casos en que ese campo contengan el caracter Ñ, ejemplo, si el nombre correspondiente es FANDIÑO JAIRO ERNESTO retorna dicho campo en null. Si hago la consulta por Id el query que hace es el siguiente

[codigo]
SELECT cedula, nombre FROM maestro WHERE nombre='PIEDRAHITA FANDI\xD1O JAIRO ERNESTO';
[/codigo]

Y la respuesta es que no existe registro

Como se puede ver hay un reemplazo del caracter Ñ por \xD1

La base de datos esta codificada con LATIN1
Versión PHP 5.2.17

Lo curioso es que si NO manejo JQuery los resultados que se obtienen son correctos, es decir, no se presenta el problema que estoy exponiendo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Cambiar character set... Como se hace?

Publicado por AJaramillo (2 intervenciones) el 29/02/2012 01:29:23
Ooootra vez yo con mi karma (jejejeje)

A ver, cuando voy a hacer una consulta por nombre (CASTAÑEDA YOLANDA) y verifico el queryString antes de enviarlo veo

1
[19:12:51.331] ced=&[COLOR="Red"]nom=CASTA%C3%91EDA+YOLANDA[/COLOR]&accion=envia


Si envio la peticion al servidor sin [COLOR="Red"]type: 'json'[/COLOR] y retornar el contenido de $_POST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$(document).ready( function () {
				$("input").change( function () {
					datos = $("#prueba").serialize()+"&accion=envia";
console.log(datos);
					$.ajax({
						data: datos,
						type: "post",
						//dataType: "json",
						//contentType: "application/json; charset=iso-8859-1",
						cache: false,
						url: "c.php",
						success: function(json) {
console.log(json);
							$.each(json.data, function (clave, valor) {
								$("#nom").val(valor.nombre);
							});
						}
					});
				});
				return false;
			});


Obtengo

1
2
3
4
5
6
7
8
[19:12:51.476]
<hr><hr><pre>Array
(
    [ced] =>
    [nom] => [COLOR="Red"]CASTAÑEDA YOLANDA[/COLOR]
    [accion] => envia
)
</pre>


y el query que hace es

1
2
[19:17:53.421]
{"sql":"SELECT cedula, nombre FROM maestro WHERE nombre='PINTO CASTA\u00d1EDA MARIA YOLANDA'"}


O sea, veo un revuelto de charset.... (???)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar