<?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>codegods &#187; JavaScript</title>
	<atom:link href="http://codegods.de/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://codegods.de</link>
	<description>where code meets experts</description>
	<lastBuildDate>Wed, 18 Apr 2012 12:42:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Synchrone und asynchrone HTTP-Requests in JavaScript</title>
		<link>http://codegods.de/2008/08/28/synchron-asynchron-http-requests-javascript/</link>
		<comments>http://codegods.de/2008/08/28/synchron-asynchron-http-requests-javascript/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 14:19:22 +0000</pubDate>
		<dc:creator>Sebastian Henke</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[asynchron]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[request]]></category>
		<category><![CDATA[synchron]]></category>
		<category><![CDATA[warten]]></category>

		<guid isPermaLink="false">http://codegods.de/?p=46</guid>
		<description><![CDATA[In JavaScript gibt es zwei Möglichkeiten HTTP-Requests zu verarbeiten. Sie können entweder synchron oder asynchron durchgeführt werden. Synchron bedeutet hier, dass die weitere Programmausführung so lange unterbrochen wird, bis der Request durchgeführt wurde und eine Anwort vorliegt. Diese Programmierung ist zwar einfacher verständlich, birgt jedoch einige Schwierigkeiten. JavaScript ist nicht multithreadingfähig. Somit können keine anderen [...]]]></description>
			<content:encoded><![CDATA[<p>In JavaScript gibt es zwei Möglichkeiten HTTP-Requests zu verarbeiten. Sie können entweder synchron oder asynchron durchgeführt werden. Synchron bedeutet hier, dass die weitere Programmausführung so lange unterbrochen wird, bis der Request durchgeführt wurde und eine Anwort vorliegt. Diese Programmierung ist zwar einfacher verständlich, birgt jedoch einige Schwierigkeiten. JavaScript ist nicht multithreadingfähig. Somit können keine anderen Operationen durchgeführt werden, solange auf einen synchronen HTTP-Request gewartet wird. Insbesondere, wenn z. B. auf einer Homepage mehrere Inhalte per HTTP-Requests nachgeladen werden sollen, kann somit das "Ausfallen" eines einzigen Requests zum Nichtladen weiterer Inhalte führen. Zudem wird wertvolle Rechenzeit (z. B. für die Vorbereitung weiterer Requests) verschwendet!</p>
<p>Um diesem Problem zu entgehen, werden besonders zu diesem Zweck in der Regel asynchrone Requests verwendet. Dies bedeutet, dass JavaScript direkt nach dem Absenden des Requests (siehe unten) nicht auf eine Antwort wartet, sondern mit der Ausführung in der nächsten Zeile fortfährt. Um das Ergebnis der Anfrage dennoch verarbeiten zu können, wird dem Request-Objekt zusätzlich eine Funktion mitgegeben, die bei einer Statusänderung des Requests ausgeführt wird. Diese asynchrone Form wird AJAX (Asynchronous JavaScript and XML) genannt und wird vermutlich dem einen oder anderen schon als "Buzzword" bekannt sein.</p>
<p>Die folgenden zwei Beispiele - einmal synchron, einmal asynchron - führen jeweils einen Request aus und geben nach erfolgreicher Abfrage mittels <em>alert</em> eine Meldung aus.</p>
<p>Synchron:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// In den ersten Zeilen wird ein HTTP-Requestobjekt erzeugt. Dieses geschieht mittels Browserweiche.</span>
<span style="color: #009900; font-style: italic;">// Der interessante Teil geschieht ab dem nächsten Kommentar.</span>
<span style="color: #003366; font-weight: bold;">var</span> xmlHttpObject = <span style="color: #003366; font-weight: bold;">false</span>;
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> XMLHttpRequest != <span style="color: #3366CC;">'undefined'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	xmlHttpObject = <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!xmlHttpObject<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		xmlHttpObject = <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Msxml2.XMLHTTP&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">try</span>
		<span style="color: #66cc66;">&#123;</span>
			xmlHttpObject = <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			xmlHttpObject = <span style="color: #003366; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Vorbereiten des Requests:</span>
xmlHttpObject.<span style="color: #000066;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'get'</span>, getUrl, <span style="color: #003366; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;  <span style="color: #009900; font-style: italic;">// HIER WICHTIG: das &quot;false &quot; sorgt für den synchronen Request!</span>
xmlHttpObject.<span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;                      <span style="color: #009900; font-style: italic;">// Request absenden. Hier wird gewartet, bis der Request erfolgreich war.</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>xmlHttpObject.<span style="color: #006600;">readyState</span> == <span style="color: #CC0000;">4</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #009900; font-style: italic;">//  Status 4 bedeutet, dass die Abfrage erfolgreich war und das Ergebnis vorliegt</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;HTTP-Request erfolgreich!&quot;</span><span style="color: #66cc66;">&#41;</span>;  <span style="color: #009900; font-style: italic;">// Das Ergebnis des Requests findet sich in xmlHttpObject.responseText</span>
<span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Request fehlgeschlagen!&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Asynchron:</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> xmlHttpObject = <span style="color: #003366; font-weight: bold;">false</span>;
<span style="color: #009900; font-style: italic;">// In den ersten Zeilen wird ein HTTP-Requestobjekt erzeugt. Dieses geschieht mittels Browserweiche.</span>
<span style="color: #009900; font-style: italic;">// Der Interessante Teil geschieht ab dem nächsten Kommentar.</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> XMLHttpRequest != <span style="color: #3366CC;">'undefined'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	xmlHttpObject = <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!xmlHttpObject<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">try</span>
	<span style="color: #66cc66;">&#123;</span>
		xmlHttpObject = <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Msxml2.XMLHTTP&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">try</span>
		<span style="color: #66cc66;">&#123;</span>
			xmlHttpObject = <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			xmlHttpObject = <span style="color: #003366; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
xmlHttpObject.<span style="color: #000066;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'get'</span>, getUrl<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// Hier kein false wie oben -&gt; Request läuft asynchron</span>
xmlHttpObject.<span style="color: #006600;">onreadystatechange</span> = setstate;            <span style="color: #009900; font-style: italic;">// HIER WICHTIG: Funktion wird gesetzt, die bei Statusänderung ausgeführt wird.</span>
xmlHttpObject.<span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> setstate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>	<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>xmlHttpObject.<span style="color: #006600;">readyState</span> == <span style="color: #CC0000;">4</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Request wurde erfolgreich verarbeitet.&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>	
&nbsp;
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;Request wurde soeben abgesendet!&quot;</span><span style="color: #66cc66;">&#41;</span>;   <span style="color: #009900; font-style: italic;">// Kaum zu glauben, aber dieses Alert wird vor dem oben in der Funktion aufgerufen!</span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://codegods.de/2008/08/28/synchron-asynchron-http-requests-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

