<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Polar Geek &#187; ajax</title>
	<atom:link href="http://www.polargeek.net/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.polargeek.net</link>
	<description>Desarrollo web, tips, diseño, php, javascript...</description>
	<lastBuildDate>Thu, 29 Jul 2010 09:33:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Recibir varios datos por Ajax con Javascript</title>
		<link>http://www.polargeek.net/javascript-recibir-varios-datos-por-ajax-post/</link>
		<comments>http://www.polargeek.net/javascript-recibir-varios-datos-por-ajax-post/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 23:01:48 +0000</pubDate>
		<dc:creator>José</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.polargeek.net/?p=989</guid>
		<description><![CDATA[Introducción Cuando se trata de recibir varios datos tras hacer una petición Ajax, surge un problema, solo se puede recibir una cadena de texto, y ya está, esto se debe a la naturaleza de Ajax, pero a eso dedicaré otro post más adelante. Con el método GET, existe una función (jQuery.getJSON), pero este método no [...]]]></description>
			<content:encoded><![CDATA[<h2>Introducción</h2>
<p><a href="http://www.flickr.com/photos/codydildy/1031912100/"><img src="http://static.polargeek.net/uploads/datos-con-ajax.jpg" alt="Golden Gate" /></a><br />
Cuando se trata de recibir varios datos tras hacer una petición Ajax, surge un problema, solo se puede recibir una cadena de texto, y ya está, esto se debe a la naturaleza de Ajax, pero a eso dedicaré otro post más adelante.</p>
<p>Con el método GET, existe una función (<em>jQuery.getJSON</em>), pero este método no es el más elegante a mi entender en muchos proyectos. (El artículo POST vs. GET está en proceso de fabricación&#8230;).</p>
<p>Así que necesitamos alguna manera de pasar X datos desde un script a una función javascript que nos ha hecho la petición Ajax.</p>
<h2>JSON</h2>
<p>Bueno amigos, JSON es nuestro aliado en este caso. JSON es simplemente una manera de juntar datos en forma de string, con un estándar, claro.</p>
<p>¿Y cuál es el punto fuerte de esto? Pues que al ser estándar en muchos lenguajes existen funciones que nos facilitan la vida (PHP, Javascript, Python, Perl&#8230;). </p>
<p>Veremos un ejemplo con PHP, aunque en realidad no importa el lenguaje, la idea se puede extrapolar con scripts de otros lenguajes siempre y cuando implementen funciones de JSON.</p>
<h2>Caso práctico</h2>
<p>En un script de PHP (<em>prueba.php</em>) tenemos el siguiente código:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #000088;">$array_datos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;direccion&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Calle Inventada&quot;</span><span style="color: #339933;">,</span>
                       <span style="color: #0000ff;">&quot;ciudad&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Hong Kong&quot;</span>
                     <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$array_datos_json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_datos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Lo codificamos en JSON</span>
 <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$array_datos_json</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Bien, tenemos la siguiente petición por Ajax, con el método POST al script anterior.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;prueba.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;func&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;conseguirDireccionCiudad&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">direccion</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Dato 1</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">ciudad</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Dato 2</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Como se puede ver se decodifica directamente con la función $.post(), que permite especificar el tipo de dato que vamos a obtener en el 4º parámetro. </p>
<h2>Documentación</h2>
<p>Jquery + Ajax | <a href="http://docs.jquery.com/Ajax">Jquery.com</a><br />
JSON | <a href="http://www.json.org/json-es.html">Documentación oficial</a><br />
JSON | <a href="http://es.wikipedia.org/wiki/JSON">Documentación útil</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.polargeek.net/javascript-recibir-varios-datos-por-ajax-post/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[Ajax] Refrescar div cada cierto tiempo</title>
		<link>http://www.polargeek.net/ajax-refrescar-div-cada-cierto-tiempo/</link>
		<comments>http://www.polargeek.net/ajax-refrescar-div-cada-cierto-tiempo/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 13:20:23 +0000</pubDate>
		<dc:creator>José</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[peticiones]]></category>

		<guid isPermaLink="false">http://www.polargeek.net/?p=974</guid>
		<description><![CDATA[Introducción Si tenemos un dato que queremos actualizar cada cierto tiempo, ya puede estar en un div o cualquier otro contenedor, lo que debemos hacer es una petición ajax establecida en un intervalo que se repetirá siempre. Aplicaciones como Twitter o Gmail ya lo hacen. En realidad hay múltiples formas de realizar esto, pero una [...]]]></description>
			<content:encoded><![CDATA[<h2>Introducción</h2>
<p>Si tenemos un dato que queremos actualizar cada cierto tiempo, ya puede estar en un div o cualquier otro contenedor, lo que debemos hacer es una petición ajax establecida en un intervalo que se repetirá siempre. Aplicaciones como Twitter o Gmail ya lo hacen. </p>
<p>En realidad hay múltiples formas de realizar esto, pero una forma fácil de hacerlo es utilizar un plugin de jquery.</p>
<h2>Timers</h2>
<p>Timers es el plugin en cuestión, y comprimido se queda en 1.54 KB, algo más que razonable. Tiene varias funciones, pero vamos a centrarnos en el uso para lo que queremos.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script language=&quot;javascript&quot; src=&quot;jquery.min.js&quot;&gt;&lt;/script&gt; //Incluimos Jquery
&lt;script language=&quot;javascript&quot; src=&quot;jquery.timers-1.0.0.js&quot;&gt;&lt;/script&gt; //Incluimos el plugin
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	j<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		j<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.refreshMe&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">everyTime</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10000</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			j.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">//Aquí se hara la petición (sin nada)</span>
			  url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;refresh-me.php&quot;</span><span style="color: #339933;">,</span> 
			  cache<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
			  success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">//Aquí refrescamos el div</span>
				j<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.refreshMe&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			  <span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   j<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.refreshMe'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>color<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;body&gt;
&lt;div class=&quot;refreshMe&quot;&gt;Esto se va a refrescar cada 10 seg&lt;/div&gt;
&lt;/body&gt;</pre></td></tr></table></div>

<p>Primero de todo incluimos Jquery y el plugin en cuestión (más abajo tienes los links para todo), y después establecemos el tiempo con <em>j(&#8220;.refreshMe&#8221;).everyTime</em>, que indica que los elementos con los selectores &#8220;.refreshMe&#8221; van a ser actualizados. También aquí establecemos el tiempo que queremos(10 segundos).</p>
<p>En la petición no se manda ningún parámetro, por simplicidad, pero se podría hacer de igual forma. </p>
<h2>Demo</h2>
<p>En <a href="http://www.thatagency.com/design-studio-blog/examples/simple-page-element-refresh/">este enlace</a>, puedes ver como funciona, simplemente se llama a la función rand de php, que genera un número &#8220;aleatorio&#8221; y se actualiza el valor en el html. </p>
<h2>Enlaces</h2>
<p>Plugin | <a href="http://plugins.jquery.com/project/timers">Repositorio Jquery</a><br />
Vía | <a href="http://www.thatagency.com/design-studio-blog/2009/01/refreshing-an-element-at-a-set-time-interval-using-jquery-and-a-sprinkle-of-ajax/">That Agency</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.polargeek.net/ajax-refrescar-div-cada-cierto-tiempo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
