﻿<?xml version="1.0" encoding="utf-8" ?> <!--  generator="FEEDCREATOR_VERSION"  --><rss version="2.0" xmlns:sns="http://www.webczw.com/sns"><channel><title>伟博小城</title><link>http://www.webczw.com/</link><description></description><lastBuildDate>Fri, 18 May 2012 16:24:46 +0800</lastBuildDate> <generator>FEEDCREATOR_VERSION</generator>  <language>zh-cn</language> <copyright>Copyright © 2010 - 2012 Weibo Little Town, All Rights Reserved</copyright> <pubDate>Fri, 18 May 2012 16:24:46 +0800</pubDate> <item><title>Ajax与JSON的一些总结</title><link>http://www.webczw.com/s/201251816244659086.html</link><description><![CDATA[<p>&nbsp;</p>
<div id="cnblogs_post_body" style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(51, 51, 51); font-family: ‘Microsoft Yahei‘, Tahoma, Arial, Helvetica, STHeiti; font-size: 15px; line-height: 32px; ">
<h1 style="margin: 15px 0px; padding: 5px 0px 5px 20px; outline: none 0px; color: white; line-height: 1.5; background-image: none; background-attachment: scroll; background-color: rgb(72, 61, 139); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; font-size: 18pt; letter-spacing: 1px; background-position: 0px 0px; background-repeat: repeat repeat; ">1.1.1 摘要</h1>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">Ajax技术的核心是<a href="http://www.w3.org/TR/XMLHttpRequest/" target="_blank" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">XMLHttpRequest</a>对象（简称XHR），可以通过使用XHR对象获取到服务器的数据，然后再通过DOM将数据插入到页面中呈现。虽然名字中包含XML，但Ajax通讯与数据格式无关，所以我们的数据格式可以是XML或JSON等格式。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">XMLHttpRequest对象用于在后台与服务器交换数据，具体作用如下：</p>
<ul style="margin: 0px 0px 0px 45px; padding: 0px; outline: none 0px; ">
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; ">在不重新加载页面的情况下更新网页</li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; ">在页面已加载后从服务器请求数据</li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; ">在页面已加载后从服务器接收数据</li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; ">在后台向服务器发送数据</li>
</ul>
<h2 style="margin: 0px; padding: 0px 0px 0px 20px; outline: none 0px; color: rgb(25, 119, 176); line-height: 1.5; font-size: 18px; ">本文目录</h2>
<ul style="margin: 0px 0px 0px 45px; padding: 0px; outline: none 0px; ">
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a href="http://www.cnblogs.com/rush/archive/2012/05/15/2502264.html#lb1" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">XMLHttpRequest的用法</a></li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a href="http://www.cnblogs.com/rush/archive/2012/05/15/2502264.html#lb2" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">Ajax同源请求</a></li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a href="http://www.cnblogs.com/rush/archive/2012/05/15/2502264.html#lb3" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">同源策略与跨源策略</a></li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a href="http://www.cnblogs.com/rush/archive/2012/05/15/2502264.html#lb4" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">Ajax跨源请求</a></li>
</ul>
<h1 style="margin: 15px 0px; padding: 5px 0px 5px 20px; outline: none 0px; color: white; line-height: 1.5; background-image: none; background-attachment: scroll; background-color: rgb(72, 61, 139); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; font-size: 18pt; letter-spacing: 1px; background-position: 0px 0px; background-repeat: repeat repeat; ">1.1.2 正文</h1>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">XMLHttpRequest是一个JavaScript对象，它是由微软设计，并且被Mozilla、Apple和Google采纳，W3C正在标准化它。它提供了一种简单的方法来检索URL中的数据。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">我们要创建一个XMLHttpRequest实例，只需new一个就OK了：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">//// Creates a XMLHttpRequest object. </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>req = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span>XMLHttpRequest();</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">也许有人会说：&ldquo;这可不行啊！IE6不支持原始的XHR对象&rdquo;，确实是这样，我们在后面将会介绍支持IE6或更老版本创建XHR对象的方法。</p>
<h2 id="lb1" style="margin: 0px; padding: 0px 0px 0px 20px; outline: none 0px; color: rgb(25, 119, 176); line-height: 1.5; font-size: 18px; ">XMLHttpRequest的用法</h2>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">在创建XHR对象后，接着我们要调用一个初始化方法open()，它接受五个参数具体定义如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">void </span>open(
   DOMString method, <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">//&quot;GET&quot;, &quot;POST&quot;, &quot;PUT&quot;, &quot;DELETE&quot;    </span>DOMString url,
   optional <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">boolean </span>async,
   optional DOMString user,
   optional DOMString password
);</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">通过上面的定义我们知道open()方法的签名包含五个参数，其中有参数method和url地址是必填的，假设我们针对URL: myxhrtest.aspx发送GET请求获取数据，具体定义如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>req = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span>XMLHttpRequest();

req.open(
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;GET&quot;</span>,
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;myxhrtest.aspx&quot;</span>,
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">false </span>);</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">通过上述代码会启动一个针对myxhrtest.aspx的GET请求，这里有两点要注意：一是URL相对于执行代码的当前页面（使用绝对路径）；二是调用open()方法并不会真正发送请求，而只是启动一个请求准备发送。</p>
<blockquote style="margin: 2em 16px; padding: 20px 40px; outline: none 0px; background-image: none; background-color: rgb(239, 244, 225); border: 1px solid rgb(205, 221, 158); border-top-left-radius: 15px; border-top-right-radius: 15px; border-bottom-right-radius: 15px; border-bottom-left-radius: 15px; quotes: ‘‘; ">
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">只能向同一个域中使用相同端口和协议的URL中发送请求；如果URL与启动请求的页面有任何差别，都会引发安全错误。</p>
</blockquote>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">要真正发送请求要使用send()方法，send()方法接受一个参数，即要作为请求主体发送的数据，如果不需要通过请求主体发送数据，我们必须传递一个null值。在调用send()之后，请求就会被分派到服务器，完整Ajax请求代码如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>req = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span>XMLHttpRequest();

req.open(
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;GET&quot;</span>,
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;myxhrtest.aspx&quot;</span>,
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">false </span>);
req.send(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">null</span>);</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">在发送请求之后，我们需要检查请求是否执行成功，首先可以通过status属性判断，一般来说，可以将HTTP状态代码为200作为成功标志。这时，响应主体内容会保存到responseText中。此外，状态代码为304表示请求的资源并没有被修改，可以直接使用浏览器缓存的数据，Ajax的同步请求代码如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>(req != <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">null</span>) {
    req.onreadystatechange = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function</span>() {

        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>((req.status &gt;= 200 &amp;&amp; req.status &lt; 300) || req.status == 304) {
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">//// Do something.         </span>}
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">else </span>{
            alert(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;Request was unsuccessful: &quot; </span>+ req.status);
        }
    };
    req.open(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;GET&quot;</span>, <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;www.myxhrtest.aspx&quot;</span>, <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">true</span>);
    req.send(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">null</span>);
}</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">前面我们定义了Ajax的同步请求，如果我们发送异步请求，那么在请求过程中javascript代码会继续执行，这时可以通过readyState属性判断请求的状态，当readyState = 4时，表示收到全部响应数据，属性值的定义如下：</p>
<div align="center" style="margin: 0px; padding: 0px; outline: none 0px; ">
<table border="1" cellspacing="0" cellpadding="0" align="center" style="margin: 0px auto; padding: 0px; outline: none 0px; font-size: 12px; line-height: 1.3; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; border-spacing: 0px; ">
    <tbody style="margin: 0px; padding: 0px; outline: none 0px; ">
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">readyState值</p>
            </td>
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">描述</p>
            </td>
        </tr>
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-color: rgb(211, 223, 237); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(122, 160, 205); border-top-width: 1px; border-top-style: solid; border-top-color: rgb(122, 160, 205); background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">0</p>
            </td>
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">未初始化；尚未调用open()方法</p>
            </td>
        </tr>
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">1</p>
            </td>
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">启动；尚未调用send()方法</p>
            </td>
        </tr>
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-color: rgb(211, 223, 237); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(122, 160, 205); border-top-width: 1px; border-top-style: solid; border-top-color: rgb(122, 160, 205); background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">2</p>
            </td>
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">已发送；但尚未收到响应</p>
            </td>
        </tr>
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">3</p>
            </td>
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">接收；已经收到部分响应数据</p>
            </td>
        </tr>
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-color: rgb(211, 223, 237); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(122, 160, 205); border-top-width: 1px; border-top-style: solid; border-top-color: rgb(122, 160, 205); background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">4</p>
            </td>
            <td valign="top" width="284" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">完成；收到全部响应数据</p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">表1 readyState属性值</p>
<blockquote style="margin: 2em 16px; padding: 20px 40px; outline: none 0px; background-image: none; background-color: rgb(239, 244, 225); border: 1px solid rgb(205, 221, 158); border-top-left-radius: 15px; border-top-right-radius: 15px; border-bottom-right-radius: 15px; border-bottom-left-radius: 15px; quotes: ‘‘; ">
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">同步请求：发生请求后，要等待服务器执行完毕才继续执行当前代码。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">异步请求：发生请求后，无需等到服务器执行完毕，可以继续执行当前代码。</p>
</blockquote>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">现在我们要增加判断readyState属性值，当readyState = 4时，表示全部数据接收完成， 所以Ajax的异步请求代码如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>(req != <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">null</span>) {
    req.onreadystatechange = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function</span>() {

        <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">//// Checks the asyn request completed or not.         </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>(req.readyState == 4) {
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>((req.status &gt;= 200 &amp;&amp; req.status &lt; 300) || req.status == 304) {
                <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">//// Do something.             </span>}
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">else </span>{
                alert(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;Request was unsuccessful: &quot; </span>+ req.status);
            }
        }
    };
    req.open(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;GET&quot;</span>, <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;www.myxhrtest.aspx&quot;</span>, <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">true</span>);
    req.send(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">null</span>);
}</pre>
<h2 id="lb2" style="margin: 0px; padding: 0px 0px 0px 20px; outline: none 0px; color: rgb(25, 119, 176); line-height: 1.5; font-size: 18px; ">Ajax同源请求</h2>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">现在我们对Ajax的请求实现有了初步的了解，接下来我们将通过具体的例子说明Ajax请求的应用场合和局限。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">在日常网络生活中，我们在浏览器的地址中输入要访问的URL并且回车，浏览器会向服务器发送请求，当服务器收到请求后，把相应的请求页面发送回浏览器，我们会发现页面大部分加载完毕，有些还没有加载完毕。总得来说，采用异步加载方式不会影响已加载完毕的页面浏览，我们可以通过Ajax实现异步加载。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">这里我们以AdventureWorks数据库为例，把产品表（Product）中的数据通过报表呈现给用户，我们可以通过多种方法实现该报表需求，这里我们将通过Ajax实现该功能。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">首先，我们要把后台数据转换为JSON格式，接下来我们定义Product表的数据库访问对象（DAO），具体的实现代码如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt; /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">The product datatable dao. </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public class </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">ProductDao </span>{
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt;     /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">Initializes a new instance of the </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">&lt;see cref=&quot;ProductDao&quot;/&gt; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">class.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt;     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public </span>ProductDao()
    {
    }

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt;     /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">Gets or sets the product id.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt;     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public int </span>Id { <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">get</span>; <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">set</span>; }

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt;     /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">Gets or sets the product name.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt;     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public string </span>Name { <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">get</span>; <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">set</span>; }

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt;     /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">Gets or sets the product serial number.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt;     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public string </span>SerialNumber { <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">get</span>; <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">set</span>; }

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt;     /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">Gets or sets the product qty.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt;     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public short </span>Qty { <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">get</span>; <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">set</span>; }
}</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">前面我们定义了Product表的数据库访问对象&mdash;&mdash;ProductDao，它包含四个属性分别是产品的Id，名称，序列号和销售数量。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">接下来，让我们实现Product表的数据库操作类。</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt; /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">Product table data access manager. </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public class </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">ProductManager </span>{
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt;     /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">The query sql.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt;     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">private const string </span>Query = 
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;SELECT ProductID, Name, ProductNumber, SafetyStockLevel FROM Production.Product&quot;</span>;

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt;     /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">Stores the object of </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">&lt;see cref=&quot;ProductDao&quot;/&gt; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">into list.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt;     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">private </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">IList</span>&lt;<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">ProductDao</span>&gt; _products = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">List</span>&lt;<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">ProductDao</span>&gt;();

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt;     /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">Gets all products in product table.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt;     /// &lt;returns&gt;     /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">The list of </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">&lt;see cref=&quot;ProductDao&quot;/&gt; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">object.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/returns&gt;     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">IList</span>&lt;<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">ProductDao</span>&gt; GetAllProducts()
    {
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">using </span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>con = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">SqlConnection</span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">ConfigurationManager</span>.ConnectionStrings[<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;SQLCONN&quot;</span>].ToString()))
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">using </span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>com = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">SqlCommand</span>(Query, con))
        {
            con.Open();
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">using </span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>reader = com.ExecuteReader(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">CommandBehavior</span>.CloseConnection))
            {
                <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">while </span>(reader.Read())
                {
                    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>product = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">ProductDao                         </span>{
                            Id = (<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">int</span>)reader[<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;ProductID&quot;</span>],
                            Name = (<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">string</span>)reader[<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;Name&quot;</span>],
                            SerialNumber = (<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">string</span>)reader[<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;ProductNumber&quot;</span>],
                            Qty = (<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">short</span>)reader[<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;SafetyStockLevel&quot;</span>]
                        };
                    _products.Add(product);
                }
            }
        }

        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">return </span>_products;
    }
}</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">前面我们实现了Product表的数据库操作类&mdash;&mdash;ProductManager，它包含两个私有字段Quey和_products，还有一个获取Product表中数据的方法&mdash;&mdash;GetAllProducts()。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">通过实现ProductDao和ProductManager，而且我们提供GetAllProducts()方法，获取Product表中的数据，接下来我们要调用该方法获取数据。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">为了使数据通过JSON格式传递给页面，这里我们要创建一般处理程序（ASHX文件），</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">一般处理程序适用场合：</p>
<ul style="margin: 0px 0px 0px 45px; padding: 0px; outline: none 0px; ">
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; ">创建动态图片</li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; ">返回REST风格的XML或JSON数据</li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; ">自定义HTML</li>
</ul>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; "><a href="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152123278501.png" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; "><img title="ajax1" border="0" alt="ajax1" src="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152123307614.png" width="705" height="477" style="margin: 0px auto; padding: 0px; outline: none 0px; border: 0px; display: block; float: none; " /></a></p>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">图1一般处理程序</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">把一般处理程序文件添加到项目中时，会添加一个扩展名为.ashx的文件，现在我们创建一个一般处理程序ProductInfo，具体代码如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; background-color: rgb(255, 238, 98); background-position: initial initial; background-repeat: initial initial; ">&lt;%</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">@ </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">WebHandler </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">Language</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;C#&quot; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">Class</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;ProductInfo&quot; </span><span style="margin: 0px; padding: 0px; outline: none 0px; background-color: rgb(255, 238, 98); background-position: initial initial; background-repeat: initial initial; ">%&gt;  </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">using </span>System.Runtime.Serialization.Json;
<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">using </span>System.Web;

<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">using </span>ASP.App_Code;

<span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;summary&gt; /// </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">The product data handler. </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: gray; ">/// &lt;/summary&gt; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public class </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">ProductInfo </span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">IHttpHandler </span>{
    
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public void </span>ProcessRequest (<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">HttpContext </span>context) {

        context.Response.ContentType = <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;application/json&quot;</span>;

        <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Creates a &lt;see cref=&quot;ProductManager&quot;/&gt; oject.         </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>manager = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">ProductManager</span>();

        <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Invokes the GetAllProducts method.         </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>products = manager.GetAllProducts();

        <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Serializes data to json format.         </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>json = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(43, 145, 175); ">DataContractJsonSerializer</span>(products.GetType());
        json.WriteObject(context.Response.OutputStream, products);
    }

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Whether can resuable by other handler or not.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">public bool </span>IsReusable {
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">get </span>{
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">return false</span>;
        }
    }
}</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">大家注意到ProductInfo类实现了IHttpHandler接口，该接口包含一个方法ProcessRequest()方法和一个属性IsReusable。ProcessRequest()方法用于处理入站的Http请求。在默认情况下，ProductInfo类会把内容类型改为application/json，然后我们把数据通过JSON格式写入输入流中；IsReusable属性表示相同的处理程序是否可以用于多个请求，这里我们设置为false，如果为了提高性能也可以设置为true。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">如下图所示，我们通过ProductInfo类成功地实现获取数据到响应流中，并且以JSON格式显示出来。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; "><a href="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152123379919.png" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; "><img title="ajax2" border="0" alt="ajax2" src="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152123404082.png" width="598" height="515" style="margin: 0px auto; padding: 0px; outline: none 0px; border: 0px; display: block; float: none; " /></a></p>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">图2 Http请求</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">当我们请求ProductInfo时， 首先它会调用ProcessRequest()方法，接着调用GetAllProducts()方法从数据库中获取数据，然后把数据通过JSON格式写入到响应流中。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">现在，我们已经成功地把数据通过JSON格式写入到响应流当中，接着我们将通过Ajax方式请求数据并且把数据显示到页面中。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">首先，我们定义方法createXHR()用来创建XMLHttpRequest对象，前面我们提到IE6或者更老的版本不支持XMLHttpRequest()方法来创建XMLHttpRequest对象，所以我们要在createXHR()方法中，增加判断当前浏览器是否IE6或更老的版本，如果是，就要通过MSXML库的一个ActiveX对象实现。因此，在IE中可能遇到三种不同版本的XHR对象（MSXML2.XMLHttp6.0，MSXML2.XMLHttp3.0和MSXML2.XMLHttp）。</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Creates a XMLHttpRequest object bases on web broswer. </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function </span>createXHR() {

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Checks whether support XMLHttpRequest or not.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">typeof </span>XMLHttpRequest != <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;undefined&quot;</span>) {
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">return new </span>XMLHttpRequest();
    }

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// IE6 and elder version.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">else if </span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">typeof </span>ActiveXObject != <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;undefined&quot;</span>) {
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">typeof </span>arguments.callee.activeXString != <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;string&quot;</span>) {
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>versions = [
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;MSXML2.XMLHttp6.0&quot;</span>,
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;MSXML2.XMLHttp3.0&quot;</span>,
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;MSXML2.XMLHttp&quot;</span>];

            <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">for </span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>i = 0; i &lt; versions.length; i++) {
                <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">try </span>{
                    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>xhr = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span>ActiveXObject(versions[i]);
                    arguments.callee.activeXString = versions[i];
                    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">return </span>xhr;
                }
                <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">catch </span>(ex) {
                    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">throw new </span>Error(ex.toString());
                }
            }
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">return new </span>ActiveXObject(arguments.callee.activeXString);
        }
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">else </span>{
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">throw new </span>Error(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;No XHR object available&quot;</span>);
        }

    }
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">return null</span>;
}

$(document).ready(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function</span>() {
    GetDataFromServer();
});</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">前面我们定义了一个比较通用的方法用来创建XMLHttpRequest对象，并且它支持IE6或更老版本创建XMLHttpRequest对象，接下来我们将通过Ajax方法请求数据。</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function </span>GetDataFromServer() {

    <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Creates a XMLHttpRequest object.     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>req = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">new </span>createXHR();
      
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>(req != <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">null</span>) {
        req.onreadystatechange = <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function</span>() {
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>(req.readyState == 4) {
                <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">if </span>((req.status &gt;= 200 &amp;&amp; req.status &lt; 300) || req.status == 304) {
                    <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">////alert(req.responseText);                     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>jsonTextDiv = document.getElementById(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;jsonText&quot;</span>);

                    <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Deserializes JavaScript Object Notation (JSON) text to produce a JavaScript value.                     </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>data = JSON.parse(req.responseText);
                    <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">for </span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>i = 0; i &lt; data.length; i++) {
                        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>item = data[i];
                        <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>div = document.createElement(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;div&quot;</span>);
                        div.setAttribute(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;class&quot;</span>, <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;dataItem&quot;</span>);
                        <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Inserts data into the html.                         </span>div.innerHTML = item.Name + <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot; sold &quot; </span>+ item.Qty + <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;; Product number: &quot; </span>+ item.SerialNumber;
                        jsonTextDiv.appendChild(div);
                    }
                }
                <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">else </span>{
                    alert(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;Request was unsuccessful: &quot; </span>+ req.status);
                }
            }
        };
        
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Sends a asyn request.          </span>req.open(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;GET&quot;</span>, <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;ProductInfo.ashx&quot;</span>, <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">true</span>);
        req.send(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">null</span>);
    }
}</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">由于前面我们介绍过Ajax发生请求的方法，所以不再重复介绍了，但我们注意到GetDataFromServer()方法中，获取responseText数据（JSON格式），然后通过<a href="http://msdn.microsoft.com/en-us/library/windows/apps/cc836466%28v=vs.85%29.aspx" target="_blank" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">parse()</a>方法把JSON格式数据转换为Javascript对象，最后把数据插入到div中，页面呈现效果如下：</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; "><a href="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152123421376.png" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; "><img title="ajax3" border="0" alt="ajax3" src="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152123448670.png" width="564" height="362" style="margin: 0px auto; padding: 0px; outline: none 0px; border: 0px; display: block; float: none; " /></a></p>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">图3 Ajax请求结果</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">现在，我们成功地把数据输出到页面当中，也许用户还会觉得用户体验不好，那么我们给就该页面增加CSS样式。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">由于时间的关系，我们已经把CSS样式定义好了，具体如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">#header </span>{
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">width</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">100%</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">margin-left</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">10px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">margin-right</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">10px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">background-color</span>:<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">#480082</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">color</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">#FFFFFF</span>;
}
<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">body </span>{
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">margin-left</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">40px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">margin-right</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">40px</span>;
}
<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">div#jsonText </span>{
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">background-color</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">#d9d9d9</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">-webkit-border-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">border-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">margin</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">10px 0px 0px 0px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">padding</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">0px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">border</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">1px solid #d9d9d9</span>;
}
<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">div.dataItem </span>{
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">font-family</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">Verdana, Helvetica, sans-serif</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">color</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">#434343</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">padding</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">10px</span>;
}
<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">div.dataItem:nth-child(2n) </span>{
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">background-color</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">#fafafa</span>;
}
<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">div.dataItem:first-child </span>{
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">-webkit-border-top-left-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">-webkit-border-top-right-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">border-top-left-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">border-top-right-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
}
<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">div.dataItem:last-child </span>{
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">-webkit-border-bottom-left-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">-webkit-border-bottom-right-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">border-bottom-left-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">border-bottom-right-radius</span>: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">6px</span>;
}</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">我们刷新一下页面，OK现在页面效果好多了。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; "><a href="http://images.cnblogs.com/cnblogs_com/rush/201205/20120515212346108.png" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; "><img title="ajax4" border="0" alt="ajax4" src="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152123481861.png" width="604" height="322" style="margin: 0px auto; padding: 0px; outline: none 0px; border: 0px; display: block; float: none; " /></a></p>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">图4 Ajax请求结果</p>
<h2 id="lb3" style="margin: 0px; padding: 0px 0px 0px 20px; outline: none 0px; color: rgb(25, 119, 176); line-height: 1.5; font-size: 18px; ">同源策略与跨源策略</h2>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">上面我们获取页面和数据都是在<b style="margin: 0px; padding: 0px; outline: none 0px; ">同源请求</b>情况下，也就是说，客户端浏览器请求的页面和数据都是属于<b style="margin: 0px; padding: 0px; outline: none 0px; ">同一域名</b>、<b style="margin: 0px; padding: 0px; outline: none 0px; ">同一端口</b>和<b style="margin: 0px; padding: 0px; outline: none 0px; ">同协议</b>。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; "><a href="http://en.wikipedia.org/wiki/Same_origin_policy" target="_blank" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">同源策略</a>：阻止从一个域上加载的脚本获取或操作另一个域上的文档属性。也就是说，受到请求的URL的域必须与当前Web页面的域相同、相同端口。这意味着浏览器隔离来自不同源的内容，以防止它们之间的操作。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; "><a href="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152123503615.png" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; "><img title="ajax6" border="0" alt="ajax6" src="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152123515369.png" width="519" height="363" style="margin: 0px auto; padding: 0px; outline: none 0px; border: 0px; display: block; float: none; " /></a></p>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">图5同源请求过程</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">在一些情况下，我们不可以避免地要地需要从其他域名或服务器中跨域请求数据，但前面提到Ajax只能向同一个域中使用相同端口和协议的URL中发送请求；如果URL与启动请求的页面有任何差别，都会引发安全错误。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; "><a href="http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing" target="_blank" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">跨源策略</a>（CORS）：是一个Web浏览器技术规范，它定义了一个方法让Web服务器允许其他域名页面访问它的资源。跨源策略定义了一个方法让浏览器和服务器可以交互决定是否允许跨源请求。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; "><a href="http://images.cnblogs.com/cnblogs_com/rush/201205/2012051521235522.png" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; "><img title="ajax5" border="0" alt="ajax5" src="http://images.cnblogs.com/cnblogs_com/rush/201205/20120515212357281.png" width="562" height="564" style="margin: 0px auto; padding: 0px; outline: none 0px; border: 0px; display: block; float: none; " /></a></p>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">图6跨源请求过程</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">大家注意到同源请求中我们使用的是JSON格式，但在跨源请求中却是使用JSONP，这时大家可能有点困惑，坦然我刚开始学习的时候也是这样的。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">首先我们必须理解JSON和JSONP的区别：JSON是一种数据格式，而JSONP像是通过一个方法名来封装JSON格式；由于浏览器允许跨源请求&lt;script&gt;资源，如我们的HTML页面代码中使用了Google的jQuery库，当我们Web程序发送跨源请求后，服务器给我们提供响应数据，但服务器无法预知接受JSON数据的方法名，所以我们要提供一个方法名。</p>
<h2 id="lb4" style="margin: 0px; padding: 0px 0px 0px 20px; outline: none 0px; color: rgb(25, 119, 176); line-height: 1.5; font-size: 18px; ">Ajax跨源请求</h2>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">跨域请求数据解决方案主要有如下解决方法：</p>
<ol style="margin: 0px; padding: 0px 0px 0px 50px; outline: none 0px; ">
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: decimal; ">JSONP方式</li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: decimal; ">表单POST方式</li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: decimal; ">服务器代理</li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: decimal; ">Html5的XDomainRequest</li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: decimal; ">Flash request</li>
</ol>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">在介绍JSONP方式解决跨域请求数据之前，首先我们看看JSONP的定义。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">JSONP（JSON with Padding）是一个非官方的协议，它允许在服务器端集成Script tags返回至客户端，通过Javascript callback的形式实现跨域访问（这仅仅是JSONP简单的实现形式）。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">由于同源策略的限制，XMLHttpRequest只允许请求当前源（域名、协议、端口）的资源，为了实现跨域请求，可以通过script标签实现跨域请求，然后在服务端输出JSON数据并执行回调函数，从而解决了跨域的数据请求。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">假设博客园提供一个API接口：http://www.cnblogs.com/hotblogs/json?，供开发者调用获取热门博文。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">这里我们可以通过两种方式调用该接口：</p>
<h3 style="margin: 0px; padding: 0px 0px 0px 25px; outline: none 0px; color: rgb(25, 119, 176); font-size: 16px; ">1. 用Javascript定义回调函数</h3>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">其实，通过Javascript定义回调函数调用该接口比较直观，我们只需告诉服务器接收数据的方法名就OK了，比如：</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">http://www.cnblogs.com/hotblogs/json? callback=myFunction</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">其中myFunction是我们在页面自定义的函数用来接收服务器回传的数据，myFunction的定义如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// The call back function. </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function </span>myFunction(data) {
    <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Your code here. </span>}</pre>
<h3 style="margin: 0px; padding: 0px 0px 0px 25px; outline: none 0px; color: rgb(25, 119, 176); font-size: 16px; ">2. 使用jQuery的Ajax方法</h3>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">假设我们想在博客中增加显示浪微博的公共微博信息，我们可以在博客中调用微博提供的API获取跨源数据，接下来，我们将使用jQuery的Ajax方法获取跨域数据。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">首先，查看微博<a href="http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3" target="_blank" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">API文档</a>找到了公共微博的API接口<a href="http://open.weibo.com/wiki/Statuses/public_timeline" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">statuses/public_timeline</a>&nbsp;获取最新的公共微博消息，它支持JSON或XML格式数据。</p>
<div align="center" style="margin: 0px; padding: 0px; outline: none 0px; ">
<table border="1" cellspacing="0" cellpadding="0" align="center" style="margin: 0px auto; padding: 0px; outline: none 0px; font-size: 12px; line-height: 1.3; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; border-spacing: 0px; ">
    <tbody style="margin: 0px; padding: 0px; outline: none 0px; ">
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">参数</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">必选</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">类型及范围</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">说明</p>
            </td>
        </tr>
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-color: rgb(211, 223, 237); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(122, 160, 205); border-top-width: 1px; border-top-style: solid; border-top-color: rgb(122, 160, 205); background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">source</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">true</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">string</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">申请应用时分配的AppKey，调用接口时候代表应用的唯一身份。（采用OAuth授权方式不需要此参数）</p>
            </td>
        </tr>
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">count</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">false</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">int，缺省值20，最大值200</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">每次返回的记录数</p>
            </td>
        </tr>
        <tr style="margin: 0px; padding: 0px; outline: none 0px; background-image: none; background-attachment: scroll; background-color: rgb(211, 223, 237); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(122, 160, 205); border-top-width: 1px; border-top-style: solid; border-top-color: rgb(122, 160, 205); background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">count</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">false</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">int，缺省值20，最大值200</p>
            </td>
            <td valign="top" width="142" style="margin: 0px; padding: 3px; outline: none 0px; text-align: left; border: 1px solid rgb(192, 192, 192); border-collapse: collapse; ">
            <p align="left" style="margin: 0px; padding: 0px; outline: none 0px; ">每次返回的记录数</p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">表2请求参数</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">上面的请求参数只有source（AppKey）是必须的，所以我们需要向微博申请AppKey，在调用API时，只需把我们的AppKey传递给接口就OK了。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">接下来让我们看一下微博数据组成，这里我们使用<a href="http://jsonviewer.stack.hu/" target="_blank" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">JSON viewer</a>查看微博的数据组成，具体数据如下：</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; "><a href="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152124029819.png" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; "><img title="ajax8" border="0" alt="ajax8" src="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152124046731.png" width="684" height="636" style="margin: 0px auto; padding: 0px; outline: none 0px; border: 0px; display: block; float: none; " /></a></p>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">图7微博JSON数据</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">通过上图，我们知道微博的数据信息很丰富，它是由一些基础数据类型和复杂数据类型user组成的，接下来我们将使用jQuery实现调用微博接口方法。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">首先，我们定义一个全局的对象，它包含三个属性分别是：numWeibo、appendTo和appKey，还有三个方法loadWeibo()、timeAgo()和clean()，具体定义如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; ">
 JQWeibo = {

     <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// The number of weibos display in the page.      // Sets the number of weibos, append class and app key.      </span>numWeibo: 15,
     appendTo: <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">‘#jsWeibo‘</span>,

     <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// The appkey you apply from weibo.      </span>appKey: YourAppKey,

     <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// The function to get weibo data.      </span>loadWeibo: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function</span>() {
     },

     <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">/**      * Convert the time to relative time.      * @return {string} relative time like &quot;8 minutes ago&quot;      */      </span>timeAgo: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function</span>(dateString) {
     },

     ify: {
         clean: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function</span>(weibo) {
             <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">return this</span>.hash(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">this</span>.at(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">this</span>.list(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">this</span>.link(weibo))));
         }
     } <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// ify  </span>};</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">上面我们定义了一个对象JQWeibo，其中loadWeibo()方法使用jQuery的Ajax方法向微博API发送跨源请求，接下来让我们实现该方法吧。</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// The function to get weibo data. </span>loadWeibo: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function</span>() {
    $.ajax({
        <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Weibo API.         </span>url: <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;http://api.t.sina.com.cn/statuses/public_timeline.json&quot;</span>,
        type: <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;GET&quot;</span>,
        dataType: <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">&quot;jsonp&quot;</span>,
        data: {
            source: JQWeibo.appKey,
            count: JQWeibo.numWeibo
        },

        <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// When the requet completed, then invokes success function.         </span>success: <span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">function</span>(data, textStatus, xhr) {

            <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Sets html structure.             </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>html =
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">‘&lt;div class=&quot;weibo&quot;&gt;‘ </span>+
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">‘&lt;a href=&quot;http://weibo.com/DOMAIN&quot; target=&quot;_blank&quot;&gt;USER&lt;/a&gt;‘ </span>+
            <span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">‘：WEIBO_TEXT&lt;div class=&quot;time&quot;&gt;AGO&lt;/div&gt;‘</span>;

            <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Appends weibos into html page.             </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">for </span>(<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">var </span>i = 0; i &lt; data.length; i++) {
                $(JQWeibo.appendTo).append(
                html.replace(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">‘WEIBO_TEXT‘</span>, JQWeibo.ify.clean(data[i].text))

                <span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">// Uses regex and declare DOMAIN as global, if found replace all.                     </span>.replace(/DOMAIN/g, data[i].user.domain)
                    .replace(/USER/g, data[i].user.screen_name)
                    .replace(<span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">‘AGO‘</span>, JQWeibo.timeAgo(data[i].created_at))
            );
            }
        }
    })
}</pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">现在，我们使用$.ajax()方法向微博API发送跨源请求，而且我们向API传递了JQWeibo.appKey和JQWeibo.numWeibo，当请求完成后，调用Success()方法把JSON数据插入的页面当中。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">页面的HTML代码如下：</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&lt;!</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">DOCTYPE </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">HTML PUBLIC </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;         &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt; &lt;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">html</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt; &lt;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">head</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt;     &lt;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">meta </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">http-equiv</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;Content-Type&quot; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">content</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;text/html;charset=UTF-8&quot;&gt;     &lt;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">title</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt;</span>Weibo Feed<span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&lt;/</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">title</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt;     &lt;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">script </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">type</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;text/javascript&quot; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">src</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js&quot;&gt;&lt;/</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">script</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt;     &lt;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">link </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">rel</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;stylesheet&quot; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">type</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;text/css&quot; </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">href</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;css/WeiboFeed.css&quot;&gt; &lt;/</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">head</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt; &lt;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">body</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt;     &lt;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">div </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: red; ">id</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">=&quot;jsWeibo&quot;&gt;&lt;/</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">div</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt; &lt;/</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">body</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt; &lt;/</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(163, 21, 21); ">html</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: blue; ">&gt;</span></pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">&nbsp;<a href="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152124091502.png" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; "><img title="ajax9" border="0" alt="ajax9" src="http://images.cnblogs.com/cnblogs_com/rush/201205/201205152124122351.png" width="371" height="596" style="margin: 0px auto; padding: 0px; outline: none 0px; border: 0px; display: block; float: none; " /></a></p>
<p align="center" style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">图8 跨源数据</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">如上图所示，我们使用$.ajax()方法调用公共微博接口，当成功获取服务器回调数据插入到我们的页面当中。</p>
<h1 style="margin: 15px 0px; padding: 5px 0px 5px 20px; outline: none 0px; color: white; line-height: 1.5; background-image: none; background-attachment: scroll; background-color: rgb(72, 61, 139); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; font-size: 18pt; letter-spacing: 1px; background-position: 0px 0px; background-repeat: repeat repeat; ">1.1.3 总结</h1>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">本文主要介绍了Ajax在同源请求适用性，但在跨源请求中其存在的局限性，进而介绍Ajax和JSONP在跨源请求下解决方法。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">回答qianlifeng关于跨源请求的几个问题：</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">1.一般的跨源不用jsonp请求为什么会报错？和同源的不都是一个请求么？（可能对ajax了解不深）</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">答：首先跨源请求的解决方法不仅仅有JSON，本文中提及了其他方法，如：表单POST方式、服务器代理、Html5的XDomainRequest和Flash request等；而你提到报错，我觉得你首先要确认数据格式是否正确。关于跨原请求和同源请求本文已经给出了介绍。</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">2.关于&ldquo;用Javascript定义回调函数&rdquo;那块看的不是很明白。传递当前页面的一个js方法给跨源服务器，为什么就能跨源请求了呢？（JSONP？） 服务端根据这个js方法做了什么操作啊？</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">答：首先我们理解JSON是一种数据格式，而JSONP像似通过一个方法名来封装JSON格式；而跨源请求不是说指定一个回调函数实现的，而是我们利用了浏览器允许跨源请求&lt;script&gt;资源，你也可以我的HTML代码中使用的是Google提供的jQuery库，这也说明了&lt;script&gt;资源可以跨源请求。当我们发送跨源请求后，服务器会返回JSONP，但服务器无法预知接受JSON数据的方法名，所有我们要把函数名告诉（传递）服务器。</p>
<pre class="code" style="margin: 0px 20px 20px; padding: 7px; outline: none 0px; white-space: pre-wrap; word-wrap: break-word; font-family: Consolas, 微软雅黑, Arial, 宋体, sans-serif; font-size: 12px; border-color: rgb(192, 192, 192); background-color: rgb(219, 234, 255); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; "><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">//JSON  </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">{</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(220, 20, 20); ">&quot;name&quot;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">:</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(220, 20, 20); ">&quot;JK_Rush&quot;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">,</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(220, 20, 20); ">&quot;id&quot;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">:</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(200, 30, 250); ">23</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">}  </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: green; ">//JSONP  </span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">func({</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(220, 20, 20); ">&quot;name&quot;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">:</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(220, 20, 20); ">&quot;JK_Rush&quot;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">,</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(220, 20, 20); ">&quot;id&quot;</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">:</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(200, 30, 250); ">23</span><span style="margin: 0px; padding: 0px; outline: none 0px; color: black; ">}); </span></pre>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">3.看你新浪微博的那个例子，是jquery的ajax对跨源做了处理？能不能说说您提到的两种跨源方式的区别或者不同的应用场景，还是随便都一样？</p>
<p style="margin: 0px auto 1.5em; padding: 0px; outline: none 0px; text-indent: 2em; ">答：是通过$.ajax()方法实现的，如果你想使用动态Javascript实现也可以；至于两种跨源的区别已经在博文中指出了。</p>
<h2 style="margin: 0px; padding: 0px 0px 0px 20px; outline: none 0px; color: rgb(25, 119, 176); line-height: 1.5; font-size: 18px; ">参考</h2>
<ul style="margin: 0px 0px 0px 45px; padding: 0px; outline: none 0px; ">
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a href="https://developer.mozilla.org/en/XMLHttpRequest" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">https://developer.mozilla.org/en/XMLHttpRequest</a></li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a href="http://www.w3schools.com/xml/xml_http.asp" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">http://www.w3schools.com/xml/xml_http.asp</a></li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a href="http://msdn.microsoft.com/en-us/library/windows/apps/cc836466%28v=vs.85%29.aspx" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">http://msdn.microsoft.com/en-us/library/windows/apps/cc836466%28v=vs.85%29.aspx</a></li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a href="http://ntesmailfetc.blog.163.com/blog/static/206287061201241011546581/" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">http://ntesmailfetc.blog.163.com/blog/static/206287061201241011546581/</a></li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a title="http://justcoding.iteye.com/blog/1366102" href="http://justcoding.iteye.com/blog/1366102" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">http://justcoding.iteye.com/blog/1366102</a></li>
    <li style="margin: 0px; padding: 0px; outline: none 0px; list-style: inherit; list-style: inherit; "><a title="http://www.queness.com/post/8567/create-a-dead-simple-twitter-feed-with-jquery" href="http://www.queness.com/post/8567/create-a-dead-simple-twitter-feed-with-jquery" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">http://www.queness.com/post/8567/create-a-dead-simple-twitter-feed-with-jquery</a></li>
</ul>
</div>
<div id="MySignature" style="margin: 30px 0px 0px; padding: 0px; outline: none 0px; color: rgb(51, 51, 51); font-family: ‘Microsoft Yahei‘, Tahoma, Arial, Helvetica, STHeiti; font-size: 15px; line-height: 32px; ">
<div class="featured" style="margin: 15px 0px; padding: 15px; outline: none 0px; background-image: none; background-attachment: scroll; background-color: rgb(85, 85, 85); border: 1px solid rgb(170, 187, 187); border-top-left-radius: 8px; border-top-right-radius: 8px; border-bottom-right-radius: 8px; border-bottom-left-radius: 8px; color: rgb(255, 255, 255); text-align: left; background-position: 0px 0px; background-repeat: repeat repeat; ">
<div class="inside" style="margin: 0px; padding: 15px; outline: none 0px; background-image: none; background-attachment: scroll; background-color: rgb(255, 255, 255); border: 1px solid rgb(170, 187, 187); border-top-left-radius: 8px; border-top-right-radius: 8px; border-bottom-right-radius: 8px; border-bottom-left-radius: 8px; color: rgb(51, 51, 51); left: 0px; overflow: hidden; background-position: 0px 0px; background-repeat: repeat repeat; ">
<table cellspacing="15px" border="0" style="margin: 0px 1.5em 0px 0px; padding: 0px; outline: none 0px; font-size: 15px; line-height: 1.3; border: 0px; border-collapse: collapse; border-spacing: 0px; ">
    <tbody style="margin: 0px; padding: 0px; outline: none 0px; ">
        <tr valign="top" style="margin: 0px; padding: 0px; outline: none 0px; text-align: left; background-image: none; background-attachment: scroll; background-position: 0px 0px; background-repeat: repeat repeat; ">
            <td style="margin: 0px; padding: 0px; outline: none 0px; border-style: none solid none none; border-right-color: rgb(204, 204, 204); "><img width="88" height="31" class="alignleft size-thumbnail wp-image-373" alt="Jackson Huang" src="http://images.cnblogs.com/cnblogs_com/rush/268781/o_cc.png" style="margin: 0px; padding: 0px; outline: none 0px; border: 0px; " /></td>
            <td style="margin: 0px; padding: 0px; outline: none 0px; border-style: none; border-right-width: medium; ">
            <h3 style="margin: 0px; padding: 0px; outline: none 0px; color: rgb(51, 51, 51); font-size: 16px; ">关于作者:</h3>
            <p style="margin: 0px; padding: 0px; outline: none 0px; ">[作者]：<a target="_blank" href="http://www.cnblogs.com/rush" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">&nbsp;JK_Rush</a>从事.NET开发和热衷于开源高性能系统设计，通过博文交流和分享经验，欢迎转载，请保留原文地址，谢谢。<br style="margin: 0px; padding: 0px; outline: none 0px; " />
            [出处]：&nbsp;<a target="_blank" href="http://www.cnblogs.com/rush/" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">http://www.cnblogs.com/rush/</a>&nbsp;<br style="margin: 0px; padding: 0px; outline: none 0px; " />
            [本文基于]:&nbsp;<a href="http://creativecommons.org/licenses/by-nc/3.0/deed.zh" title="Attribution-NonCommercial 3.0 Unported" target="_blank" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">署名-非商业性使用 3.0</a>&nbsp;许可协议发布，欢迎转载，演绎，但是必须保留本文的署名<a target="_blank" href="http://www.cnblogs.com/rush/" style="margin: 0px; padding: 0px; outline: none 0px; text-decoration: none; color: rgb(255, 119, 0); border: 0px; ">JK_Rush</a>&nbsp;（包含链接），且不得用于商业目的。如您有任何疑问或者授权方面的协商，请与我联系 。</p>
            </td>
        </tr>
    </tbody>
</table>
</div>
</div>
</div>
<div id="blog_post_info_block" style="margin: 20px 0px 0px; padding: 0px; outline: none 0px; color: rgb(51, 51, 51); font-family: ‘Microsoft Yahei‘, Tahoma, Arial, Helvetica, STHeiti; font-size: 15px; line-height: 32px; ">
<div id="blog_post_info" style="margin: 0px; padding: 0px; outline: none 0px; ">
<div id="BlogPostCategory" style="margin: 0px; padding: 0px; outline: none 0px; ">&nbsp;</div>
</div>
</div>]]></description><author>Cawei</author><category>编程学习</category><comments>http://www.webczw.com/s/201251816244659086.html#post</comments><pubDate>Fri, 18 May 2012 16:23:49 +0800</pubDate><guid>http://www.webczw.com/s/201251816244659086.html</guid></item><item><title>高性能Web服务器 Nginx</title><link>http://www.webczw.com/s/201251717213462494.html</link><description><![CDATA[<p>&nbsp;<input type="image" src="http://www.oschina.net/uploads/img/200810/16085207_F8JL.gif" /></p>
<p>Nginx（发音同 engine x）是一款轻量级的Web 服务器／反向代理服务器及电子邮件（IMAP/POP3）代理服务器，并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发，最初供俄国大型的入口网站及搜寻引擎Rambler（俄文：Рамблер）使用。 &nbsp;其特点是占有内存少，并发能力强，事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有：新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。</p>]]></description><author>Cawei</author><category>编程学习</category><comments>http://www.webczw.com/s/201251717213462494.html#post</comments><pubDate>Thu, 17 May 2012 17:19:55 +0800</pubDate><guid>http://www.webczw.com/s/201251717213462494.html</guid></item><item><title>Novell推Mono for Android平台开发.NET</title><link>http://www.webczw.com/s/201251717073961659.html</link><description><![CDATA[<p><strong>&nbsp;2011年4月8日，北京 </strong>&mdash;&mdash; &nbsp;Novell于近日宣布推出Mono&reg; for Android，这是业界首个使用Microsoft Visual Studio为Android平台开发Microsoft .NET应用的解决方案。随着在现有Mono开发工具中加入Mono for Android，Novell使得使用Visual Studio 和其它环境的Microsoft .NET 和 C#开发人员可利用一个公共代码库轻松地为业界最广泛使用的移动设备创建应用，其中包括基于Android的智能手机和平板电脑、Apple iPad、Apple iPhone 和 Apple iPod Touch。</p>
<div>&nbsp;</div>
<div>根据市场研究公司Nielson发布的统计数据，Android占有29%的消费市场份额，是最受欢迎的智能手机平台。利用Mono for Android，.NET 开发人员和独立软件厂商（ISV）可利用Visual Studio和他们现有的技能来构建大量基于Android的应用，并向这一广大市场销售他们的产品。</div>
<div>&nbsp;</div>
<div>&ldquo;自从2009年MonoTouch引入后，开发人员已经体验到了Mono给移动应用开发工作带来的简化和效率。&rdquo; Mono项目创始人、Novell公司开发人员平台副总裁Miguel de Icaza说。&ldquo;因此，许多人要求我们为Android构建一个类似的工具。我们开发出Mono for Android，为个体开发人员和公司带来一种跨多种移动平台共享他们的代码的方法，充分利用他们在C# 和 .NET方面的专长的同时，还提升了效率。&rdquo;</div>
<div>&nbsp;</div>
<div>Mono for Android由Mono内核运行时间、原始Android 应用程序接口绑定、一个用于开发Android应用的Visual Studio 2010插件和一个包含有构建、调试和部署应用所需要的各种工具的软件开发工具包组成。经过Microsoft Visual Studio培训的开发人员可继续使用他们喜欢的IDE，同时使用他们现有的技能和.NET代码、库和工具、以及C#编程知识，为基于Android的设备创建移动应用。利用Visual Studio 2010插件，软件工程师们可开发、调试和向Android模拟器、Android设备或Android应用商店部署他们的应用。</div>
<div>&nbsp;</div>
<div>Mono for Android是对MonoTouch的一种补充。MonoTouch是Novell的一个用于iPad、 iPhone 和 iPod Touch应用开发的解决方案，备受业界好评。利用Mono for Android 和 MonoTouch，开发人员可在iPhone、iPad、iPod Touch 、Android 手机和平板电脑，以及Windows Phone 7, Windows 台式机和 Windows服务器之间共享公用代码，从而节省大量时间和金钱。Mono for Android附加项也使MonoDevelop用户可在OS X上面进行开发。</div>
<div>&nbsp;</div>
<div>&nbsp;&ldquo;作为移动软件开发商和中间件厂商，我们的客户能够使用我们的产品来开发MonoTouch，以及现在可为Android 和 Apple设备开发Mono for Android应用。&rdquo; Resco公司高级开发师Michal Sartoris说。&ldquo;我们公司拥有3000多客户，业务的成功全赖于我们比竞争者更早地向市场推出特性丰富、高质量的应用。Novell为我们提供了创新的、交叉平台开发工具，使我们能够降低和减少创建移动应用所需的成本和资源。&rdquo;</div>]]></description><author>Cawei</author><category>编程学习</category><comments>http://www.webczw.com/s/201251717073961659.html#post</comments><pubDate>Thu, 17 May 2012 17:05:28 +0800</pubDate><guid>http://www.webczw.com/s/201251717073961659.html</guid></item><item><title>C# LinQ 与 ADO.NET</title><link>http://www.webczw.com/s/201251023361284972.html</link><description><![CDATA[<div>在本文LINQ中,匿名类型广泛使用与查询表达式中的select子句,它们返回查询序列中每个元素属性的子集.</div>
<div>在本文中ADO.NET，指定DbDataAdapter所生成的DataTable。</div>
<div>操作为两个DataTable的查询操作或两个IList的查询操作。</div>
<div>场景主从表比对操作：</div>
<div>上传的数据可能存在版本不一致，基础信息都不会有变化但扩展的表或字段会不存在，原因是客户端存在没有升级的情况。</div>
<div>系统从Access数据库文件中取数据，使用整合后把相关数据并统计后对数据进行入库到系统数据库。部分的字段不能直接入库需要进行转换处理。由于数据库数据在进行操作时已经不会产生任何的变化。可以把数据都预先读取到内存当中。从而产生数据临时存放容器选择为IList和DataTable选择。</div>
<div>表A为主表：外表操作以表A为切入口, 根据表1 的某人字段从而选择当前记录行的子信息来源，关联字段要用到两个字段才能唯一。主表对从表的关系为：一对多的关系</div>
<div>表B为从表1：</div>
<div>表C为从表2：</div>
<div>把数据源转成实体操作</div>
<div>好处：操作直观，操作的字段不用每次比较时都进行比较。</div>
<div>缺点：性能不高。一个月的数据上百条记录用时几秒，一年的数据上几千条记录统计整理用时5分钟。数据量越多性能越明显。</div>
<div>ADO.NET</div>
<div>直接把表数据都查询出来没有任何过滤条件。在进行从表查询时不进行对实际的数据库文件进行操作。</div>
<div>好处：通过主表查询从表的记录信息在性能消耗并不高。同一文件一个月数据用时1秒之内，一年数据10秒之内。</div>
<div>缺点：操作并不直观，每次比较都要进行强制转换格式。后期有业务规则变化不好处理。</div>
<div>采用支持关联查询的ORM框架</div>
<div>好处：不用处理再次查询的操作，而且能用实体操作更为直观。</div>
<div>缺点：市面上没有支持Access的ORM框架，而且一般流行的ORM框架都以配置文件使用。不方便动态变化的上传文件名。</div>
<div>&nbsp;</div>
<div>现在项目处理方案：</div>
<div>由于方案三先使用起来比较麻烦要自己好写底层类。Ado.net做操作查询然后转为实体进行统计。发现真实使用时和直接采用方案二的时间一样。原因可能是从表查询才是性能的主要瓶颈，转为实体不是并不是什么性能问题。</div>
<div>如果采用方案三的方式又可以在查询DataTable这个处提高更多的性能。并且减少浪费内存资源不像现有方案用了同一数据占用了两份资源。</div>
<div>&nbsp;</div>
<div>备注：为什么没有真实的数据报告。主要当时没有想到要写这篇文档，就没有把当时使用的数据保留下来。不能一味听到别人说那个好那个不好那跟着别人走更多的时候是要有实践。个人觉得现在的ORM框架是很好用很方便逻辑和代码的处理，但遇到现实中的情况就有点力不从心（如表多了少了、字段多了少了等等）。更多时还要自己写处理方案来确保性能。还真的很久没写博客了这编的主要是体现个思想和不要人云亦云。</div>]]></description><author>Cawei</author><category>编程学习</category><comments>http://www.webczw.com/s/201251023361284972.html#post</comments><pubDate>Thu, 10 May 2012 23:35:35 +0800</pubDate><guid>http://www.webczw.com/s/201251023361284972.html</guid></item><item><title>软件研发之管理债</title><link>http://www.webczw.com/s/201251023251084310.html</link><description><![CDATA[<p>&nbsp;这是即将在Scrum Gathering上海2012大会http://scrumgathering.cn上演讲话题《软件研发中的管理债与债管理》的第一部分。如果你是一位软件研发人员，想了解软件研发为何如此困难，怎样才算做好软件；如果你是一位管理者，想知道哪些管理措施可以奏效，为什么一些管理措施效果不佳；如果你是一名敏捷精益实践者，想深入了解敏捷精益背后的思维模式。你可以来看看，以债出发的视角也许会给你带来惊喜。</p>
<div>&nbsp;</div>
<div>在软件研发组织中&ldquo;管理债&rdquo;不断堆积，这是因为采用了与软件研发的特点不匹配的管理模式。</div>
<div>&nbsp;</div>
<div><strong>软件研发管理债是什么？</strong></div>
<div>&ldquo;技术债&rdquo;是一个隐喻，这几年已经被大家熟悉并接受。如果向一个软件系统增加功能时需要额外的工作量或难度，那么这个软件系统存在技术债，这些额外的部分就是为技术债支付的利息。</div>
<div>受到&ldquo;技术债&rdquo;的启发，在此提出软件研发&ldquo;管理债&rdquo;的概念。当一个软件研发组织达成软件研发目标时需要额外的工作量或难度，我们就认为该软件研发组织存在管理债，这些额外的部分就是为&ldquo;管理债&rdquo;支付的利息。</div>
<div>软件研发&ldquo;管理债&rdquo;普遍存在于软件研发组织中。随着时间的流逝，债务从开始的小瑕疵变成庞然大物，越来越成为生命不能承担之重。其在软件研发管理上的主要表现为：1）成本越来越难以控制，超出预算；2）达成目标的时间越来越长，超出时间；3）开发出的软件不能达到市场期望，不符合需求；4）质量低下；5）软件越发庞大，难以维护。</div>
<div>谁应该为&ldquo;管理债&rdquo;负责？当从软件研发管理的角度去看时，似乎管理应该为此负责。（画外音：命名为管理债，肯定是管理负责啦，这种提法有诱导性，说服力不足。）</div>
<div>&ldquo;管理债&rdquo;与40多年前的软件危机非常类似。软件危机的主要表现是：1）软件常常超出预算；2）软件研发超出时间；3）软件不符合需求；4）软件质量低下；5）难以维护。为了应对软件危机，对应的解决方案是软件工程。软件工程是研究和应用如何以系统性的、规范化的、可定量的过程化方法去开发和维护软件，以及如何把经过时间考验而证明正确的管理技术和当前能够得到的最好的技术方法结合起来的学科。然而40多年过去了，仍然没有一种得到证明的软件工程方法能够解决软件危机的问题，它仍然以&ldquo;管理债&rdquo;的形式大量存在。</div>
<div>&nbsp;</div>
<div><strong>是何种假设导致问题在持续？</strong></div>
<div>如果方向是正确的，那么顺着方向走，找到答案只是时间问题。然而，方向不正确的时候再多的努力也不能真正解决问题，就如牛顿力学与相对论，欧几里得几何与非欧几何。</div>
<div>我们认为的正确往往都建立在假设上，如果假设不成立，那么结论亦不成立。那软件工程、软件研发管理对应的假设是什么？</div>
<div>先来审视管理与软件工程的定义。管理是管理者和他人及透过他人有效率且有效能地完成活动的程序。管理就是计划、组织、领导、协调和控制。软件工程是研究和应用如何以系统性的、规范化的、可定量的过程化方法去开发和维护软件，以及如何把经过时间考验而证明正确的管理技术和当前能够得到的最好的技术方法结合起来的学科。再观察大多数软件研发组织的做法，常见的包括：以产出为导向、明确的分工、规范的过程、精确到人的绩效管理、计划管理和不断积累的最佳实践过程资产。</div>
<div>可以观察到一个核心假设，那就是：假设存在一种正确的管理方式，这种管理方式能够规范的、定量的解决软件研发中的问题，我们的工作就是积累经验，找到该管理方式，不断修正我们的行为直至完美遵循为止。这个假设是否真的成立？</div>
<div>&nbsp;</div>
<div><strong>在软件研发中该假设不成立</strong></div>
<div>假设存在正确的管理方式，假设可以通过系统性的、规范化的、可定量的过程化方法进行软件研发，这是软件工程得以存在的基础。然而，这一假设在软件研发中并不成立。</div>
<div>定量、规范、系统性、过程、管理，这一切是典型的现代管理思路。而现代管理是建立在物质经济的基础上的。物质具备三大特点，首先是可衡量性，产出和中间产物都是物质的，可衡量的；其次是不变性，除加工步骤外，中间产物在传递过程中保持不变；最后是不相关性，产出与原材料不相关，第100个产出和第一个产出间不相关。因为产出可测量，并且不同产出间的生产过程相关性小，可重复性高，从而提升产出效率成为现代管理的核心追求；传递无损耗有利于进行步骤分解，步骤分解后可对步骤进行单独的测量和效率优化，使得精细化分工成为可能，这有带来了整体产出效率的提升。简单总结一下，现代管理的核心思想就是&ldquo;衡量是基础，产出是追求，分工是核心&rdquo;。一个典型的例子就是：澳大利亚生产铁矿石，在中国粗炼，到日本精炼，在德国加工成零部件，在美国生产成精密仪器，卖到中国。</div>
<div>现代管理对知识经济的代表-软件研发并不适用，知识的特点与物质截然不同。首先是产出的不可衡量性，知识没有体积、重量和其他任何可见的测量方式，知识与知识间无法直接比较，知识的价值取决于使用这些知识的人而不是生产这些知识的人；其次是高损耗，知识存在与大脑之中，必须转换为信息后才能进行传递，而接收者需要从外界获取信息并将之转换为自己的知识，这一过程存在巨大损耗；最后是强相关性，开发第100个功能和前面的99个功能存在巨大关联，这大大有别于物质生产。这三大特点打破了现代管理的基础，因为这三大特点，产出不可衡量导致分步骤优化的作用受到质疑，如果强行优化仅能带来局部优化，全局作用不一定明显；巨大的传递损耗使得步骤分解变得不合理，使用者决定价值的特点使得步骤间无法隔离，步骤划分难以执行，并且无法衡量步骤划分的价值，这使得建立在步骤划分上的分工不再可行；产出间的强相关性导致做的越多，负担越重，以后做的越慢，所以用做的更多来进行产出衡量意义不大。&ldquo;衡量是基础，产出是追求，分工是核心&rdquo;这些现代管理的核心思想被打破，以现代管理思想驱动的软件研发管理迟早陷入困境。</div>
<div>&nbsp;</div>
<div><strong>软件研发管理债来源于不匹配的管理模式</strong></div>
<div>将基于物质经济的现代管理应用于知识经济的代表-软件研发是软件研发组织的常见做法，这必将带来软件研发管理债的不断增长。软件研发管理需要不一样的管理模式，这种管理模式应该是什么呢？可否从管理思想和软件研发方法的发展上获得一些启示？后续大家来共同探讨吧。</div>
<div>&nbsp;</div>
<div><strong>相关链接，摸索的足迹</strong></div>
<div>从2月份提出管理债到现在，跌跌撞撞摸索之中。重新学习和摸索债、管理、软件工程、经济等基础概念，这种不断学习的感觉实在是太好了，一些博文记录了前期的收获。</div>
<div>《得懂点财务 - 从房奴养成四部曲到管理债》http://www.ituring.com.cn/article/1192</div>
<div>《管理是资产？不，管理是负债》http://www.ituring.com.cn/article/1204</div>
<div>《管理是这样从资产变成负债的》http://www.ituring.com.cn/article/1258</div>]]></description><author>Cawei</author><category>编程学习</category><comments>http://www.webczw.com/s/201251023251084310.html#post</comments><pubDate>Thu, 10 May 2012 23:23:27 +0800</pubDate><guid>http://www.webczw.com/s/201251023251084310.html</guid></item><item><title>Instagram估值10亿美元有泡沫？看看近十年30大收购案的数据对比</title><link>http://www.webczw.com/s/201241122354181341.html</link><description><![CDATA[<p>&nbsp;<a href="http://img01.36krcnd.com/wp-content/uploads/2012/04/Instagram-founders-Kevin-Systrom-left-and-Mike-Krieger.jpg" target="_blank" style="font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; font-size: 13px; line-height: inherit; text-align: center; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(42, 133, 232); text-decoration: none; outline-width: 0px; outline-style: initial; outline-color: initial; "><img class="size-full wp-image-98987" title="Instagram founders Kevin Systrom (left) and Mike Krieger" src="http://img02.36krcnd.com/wp-content/uploads/2012/04/Instagram-founders-Kevin-Systrom-left-and-Mike-Krieger.jpg" alt="" width="612" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; max-width: 678px; height: auto; " /></a></p>
<div id="attachment_98987" class="wp-caption aligncenter" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; text-align: center; color: rgb(85, 85, 85); font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; font-size: 13px; line-height: 18px; width: 622px; ">
<p class="wp-caption-text" style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; ">Instagram的两位创始人，Kevin Systrom (左)与Mike Krieger</p>
</div>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">昨天无疑是属于Instagram的。一个由13人团队做的手机拍照分享应用，发布五百多天后被Facebook以约10亿美元<a title="Facebook以总值约10亿美元的现金和股票收购拍照分享应用Instagram" href="http://www.36kr.com/p/98250.html" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(42, 133, 232); text-decoration: none; outline-width: 0px; outline-style: initial; outline-color: initial; line-height: inherit; ">收购</a>，确实令人惊讶。大多数人看到的似乎只有10亿美元与13个人，所以对这笔交易感觉疑惑，也有观点认为估值存在&ldquo;泡沫&rdquo;。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">怎样判定是不是泡沫呢？方法及标准可能各不相同，我们在这里把近10年互联网行业前30个大收购案的数据拿来做个简单对比，最后也能得出一个简单的结论。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">这些数据由<a href="http://www.wired.com/epicenter/2012/04/opinion-baio-instagram-trend/" rel="external nofollow" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(42, 133, 232); text-decoration: none; outline-width: 0px; outline-style: initial; outline-color: initial; line-height: inherit; ">Wired</a>作者安迪&middot;拜奥（Andy Baio）利用网络搜集整理。其中包括雅虎收购Broadcast.com，Zynga购入OMGPop，以及最新的Facebook对Instagram收购交易等。具体内容包括收购日期、美元金额、用户数量等数据。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">需要提醒的是，所有这些数据都来自互联网的搜索结果，包括旧新闻、Quora问答以及科技博客。因此也不保证与官方数据没有出入，但是欢迎补充和指正。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; text-align: center; "><a href="http://img03.36krcnd.com/wp-content/uploads/2012/04/Famous-Technology-Acquisitions.png" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(42, 133, 232); text-decoration: none; outline-width: 0px; outline-style: initial; outline-color: initial; line-height: inherit; "><img class="aligncenter  wp-image-98988" title="点击查看大图" src="http://img04.36krcnd.com/wp-content/uploads/2012/04/Famous-Technology-Acquisitions.png" alt="" width="722" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; max-width: 678px; height: auto; display: block; " /></a></p>
<h4 style="margin-top: 0px; margin-right: 0px; margin-bottom: 3px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(24, 24, 24); line-height: 1.25; font-size: 2.1rem; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">每个用户的收购成本</h4>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">创业公司转让的内容包括所有的技术组合、人才以及所有用户。如果按照获得每个用户的成本计算，在收购Instagram的交易中，Facebook为3500万Instagram用户每人支付了28美元。下面图表中所有收购案中，此项平均值约为92美元。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; "><a href="http://img05.36krcnd.com/wp-content/uploads/2012/04/instagram_cost_per_user.png" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(42, 133, 232); text-decoration: none; outline-width: 0px; outline-style: initial; outline-color: initial; line-height: inherit; "><img class="aligncenter size-full wp-image-98989" title="instagram_cost_per_user" src="http://img01.36krcnd.com/wp-content/uploads/2012/04/instagram_cost_per_user.png" alt="" width="647" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; max-width: 678px; height: auto; text-align: center; display: block; " /></a></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">相比之下，Aardvark（555美元每个用户）或Jaiku（240美元每个用户）收购案更像是为技术或人才而为之。1999年雅虎对Broadcast.com的收购案中每用户花费成本最高，几乎达到1.1万美元。十倍于排第二位的雅虎对Geocities的收购案，以至于上面的柱状图都没办法把它画出来。</p>
<h4 style="margin-top: 0px; margin-right: 0px; margin-bottom: 3px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(24, 24, 24); line-height: 1.25; font-size: 2.1rem; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">每个员工的收购成本</h4>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">正是这一点让人觉得此项交易存在泡沫，因为在这笔交易中获得每个员工的成本完全打破记录，Instagram的13个员工平均每人至少能分到7500万美元。不过有消息称他们的两位创始人拥有其中的5亿美元，所以平均每人能拿到多少只具有对比意义。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; "><a href="http://img02.36krcnd.com/wp-content/uploads/2012/04/COST-PER-EMPLOYEE.png" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(42, 133, 232); text-decoration: none; outline-width: 0px; outline-style: initial; outline-color: initial; line-height: inherit; "><img class="aligncenter size-full wp-image-98990" title="COST PER EMPLOYEE" src="http://img03.36krcnd.com/wp-content/uploads/2012/04/COST-PER-EMPLOYEE.png" alt="" width="650" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; max-width: 678px; height: auto; text-align: center; display: block; " /></a></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">该项对比的亚军是YouTube收购案，2006年Google花2400万美元收编了67名YouTube创始员工。在此项中，平均每个员工的收购成本约为300万美元。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">虽然有人指出，这正是有泡沫的表现。但安迪却认为，这只能说明Instagram应用架构具备难以置信的可扩展性。即使为一夜暴增的百万新用户提供服务支持，工程师也只需完成很少的额外工作。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">也就是说，优秀的应用架构使得这个Instagram无需过多的人员维护，但这并不能成为低估它本身价值的理由。</p>
<h4 style="margin-top: 0px; margin-right: 0px; margin-bottom: 3px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(24, 24, 24); line-height: 1.25; font-size: 2.1rem; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">每位员工服务用户数量对比</h4>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">此项对比可以对效率进行很好的对比。对各位来说，Instagram是那种理想中的社交网络 &mdash; 用户多，员工少。平均每位员工可服务207万用户，如此高效，怎能不出类拔萃。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">每位员工服务用户数量第二多的是OMGPOP，他们开发出了目前为止增速最快的移动应用 &mdash; Draw Something。仅仅用了50天时间用户便增至5000万，平均每位员工服务87.5万用户。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">此项效率最低的两家公司是问答服务Aardvark及消费者服务巨头Zappos，每个员工分别服务的平均用户数为1800名和3400名。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">相较而言，该应用系统的效率更出色：具备大规模的承载能力，却只需非常小的工程量。从每位用户的收购成本来看，Instagram收购案不存在估值泡沫；虽然每位员工的收购成本看似很高，但考虑到员工及系统的效率，也不成为对其低估的理由。</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 18px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; line-height: 26px; color: rgb(68, 68, 68); font-size: 16px; font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; ">因此，采用这种评估方式的结论就是：Instagram收购案中估值不存在泡沫。当然，可能你采用更专业的评估方式得出的结论与此相悖。那也没有关系，此处只是抛砖引玉，欢迎发表你的观点。</p>
<div class="wumii-hook" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(85, 85, 85); font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; font-size: 13px; line-height: 18px; ">&nbsp;</div>
<div class="show-on-desktops" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(85, 85, 85); font-family: ‘Microsoft Yahei‘, ‘Helvetica Neue‘, ‘Luxi Sans‘, ‘DejaVu Sans‘, Tahoma, ‘Hiragino Sans GB‘, STHeiti; font-size: 13px; line-height: 18px; ">文章来自<a rel="bookmark" title="Instagram估值10亿美元有泡沫？看看近十年30大收购案的数据对比" href="http://www.36kr.com/p/98982.html" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font: inherit; vertical-align: baseline; color: rgb(42, 133, 232); text-decoration: none; outline-width: 0px; outline-style: initial; outline-color: initial; line-height: inherit; ">36氪</a></div>]]></description><author>Cawei</author><category>编程学习</category><comments>http://www.webczw.com/s/201241122354181341.html#post</comments><pubDate>Wes, 11 Apr 2012 22:34:26 +0800</pubDate><guid>http://www.webczw.com/s/201241122354181341.html</guid></item><item><title>人为什么要活着？</title><link>http://www.webczw.com/s/20124122140980049.html</link><description><![CDATA[<p>6个台湾人，平均年龄81岁，环岛一周，1139公里。只为了一个简单的理由。。。<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; line-height: 20px; ">希望一生都這樣有激情 人爲什麽要活著 不管18歲 還是81歲 我們行走在人生的旅途上。</span><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; line-height: 20px; ">活着，不是为了受罪，不是为了活着，而是有没有值得你值得为他而活的人，有没有你值得努力生活的理由和信念</span></p>
<p><object width="640" height="506">
<param name="movie" value="http://share.vrs.sohu.com/my/v.swf&amp;topBar=1&amp;id=15734507&amp;autoplay=false" />
<param name="allowFullScreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="wmode" value="Transparent" /><embed width="640" height="506" wmode="Transparent" allowfullscreen="true" allowscriptaccess="always" quality="high" src="http://share.vrs.sohu.com/my/v.swf&amp;topBar=1&amp;id=15734507&amp;autoplay=false" type="application/x-shockwave-flash"></embed></object></p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></description><author>Cawei</author><category>其他分类</category><comments>http://www.webczw.com/s/20124122140980049.html#post</comments><pubDate>Sun, 01 Apr 2012 22:09:11 +0800</pubDate><guid>http://www.webczw.com/s/20124122140980049.html</guid></item><item><title>夜夜夜夜</title><link>http://www.webczw.com/s/201232417482664106.html</link><description><![CDATA[<p>&nbsp;想问天你在哪里</p>
<div>我想问问我自己</div>
<div>一开始我聪明</div>
<div>结束我聪明</div>
<div>聪明得几乎的毁掉了我自己</div>
<div>&nbsp;</div>
<div>想问天问大地</div>
<div>或者是迷信问问宿命</div>
<div>放弃所有 抛下所有</div>
<div>让我飘流在安静的夜夜空里</div>
<div>&nbsp;</div>
<div>我不愿被情捉弄</div>
<div>捉弄他让我无法大步走路</div>
<div>也不愿再多说</div>
<div>灵魂也有了裂缝</div>
<div>&nbsp;</div>
<div>你也不必牵强再说爱我</div>
<div>反正我的灵魂已片片凋落</div>
<div>慢慢的拼凑 慢慢的拼凑</div>
<div>拼凑成一个完全不属于真正的我</div>
<div>&nbsp;</div>
<div>我不愿再放纵</div>
<div>我不愿每天每夜每秒飘流</div>
<div>也不愿再多问再多说再多求</div>
<div>我的梦</div>
<div>&nbsp;</div>
<div><img width="600" height="338" alt="" src="/userfiles/image/wz_imgs/DSCF2249.jpg" /></div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<embed src="http://www.xiami.com/widget/7527436_45808,_600_200_FF8719_494949_1/multiPlayer.swf" type="application/x-shockwave-flash" width="600" height="200" wmode="opaque"></embed>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp;</div>]]></description><author>Cawei</author><category>音乐收录</category><comments>http://www.webczw.com/s/201232417482664106.html#post</comments><pubDate>Sat, 24 Mar 2012 17:43:41 +0800</pubDate><guid>http://www.webczw.com/s/201232417482664106.html</guid></item><item><title>找回属于我们的1999</title><link>http://www.webczw.com/s/201232417285962939.html</link><description><![CDATA[<p>每个人都有属于自己的童年，不管它是快乐的，还是令人厌烦的，它都使我们一生难以忘怀。</p>
<p>童年反映了我们儿时的喜怒哀乐，我想这么些个喜怒哀乐早已在我们幼小、纯洁的心灵上留下了深刻的印象。</p>
<p>&nbsp;</p>
<p><embed src="http://player.youku.com/player.php/sid/XMzU2MjgwMzMy/v.swf" allowfullscreen="true" quality="high" width="480" height="400" align="middle" allowscriptaccess="always" type="application/x-shockwave-flash"></embed></p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></description><author>Cawei</author><category>生活随笔</category><comments>http://www.webczw.com/s/201232417285962939.html#post</comments><pubDate>Sat, 24 Mar 2012 17:25:28 +0800</pubDate><guid>http://www.webczw.com/s/201232417285962939.html</guid></item><item><title>您最喜欢四季里的哪一季</title><link>http://www.webczw.com/s/201222917261062770.html</link><description><![CDATA[<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;从春分经夏至到秋分，北半球处于夏半年，南半球处于冬半年。在此期间，北半球昼长夜短，南半球昼短夜长；北极处于极昼，南极处于极夜；北回归线以北的太阳高度始终大于平均值，南回归线以南则小于平均值。北回归线以北太阳升起于东北方的地平圈上，降落于西北方的地平圈上。二分日全球各地太阳均升起于正东方，降落于正西方。</p>
<div>　　从秋分经冬至到春分，北半球处于冬半年，南半球处于夏半年。在此期间，南北半球的昼夜长短、极昼极夜和太阳高度，都同上述情况相反。北回归线以北太阳升起于东南方的地平圈上，降落于西南方的地平圈上。</div>
<p><iframe width="100%" height="418" frameborder="0" scrolling="no" src="http://vote.weibo.com/widget?vid=1500442&amp;skin=1&amp;isCustom=0&amp;height=416&amp;isAuto=1&amp;isTitle=1&amp;isBorder=1&amp;isLogo=1&amp;pad=20&amp;isResult=1"></iframe></p>]]></description><author>Cawei</author><category>生活随笔</category><comments>http://www.webczw.com/s/201222917261062770.html#post</comments><pubDate>Wes, 29 Feb 2012 17:21:35 +0800</pubDate><guid>http://www.webczw.com/s/201222917261062770.html</guid></item></channel></rss>
