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

<channel>
	<title>潘锦的空间 &#187; Ajax</title>
	<atom:link href="https://www.phppan.com/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sun, 12 Apr 2026 03:47:23 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>Ajax与REST架构简单示例</title>
		<link>https://www.phppan.com/2010/10/ajax-rest/</link>
		<comments>https://www.phppan.com/2010/10/ajax-rest/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 01:36:30 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1029</guid>
		<description><![CDATA[话说某日有见54chen真人秀，在六体膜拜之后，鉴于之前也有看过REST的一些东东，于是便有了本文，正文如下： [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>话说某日有见54chen真人秀，在六体膜拜之后，鉴于之前也有看过REST的一些东东，于是便有了本文，正文如下：<br />
REST(Representational State Transfer表述性状态转移)是一种体系架构，它为客户端和服务器之间的数据交互提供了指导。它将客户/服务器通信这种计算模型抽象到了Web层面。<br />
REST最早是在Roy Thomas Fielding博士的博士论文中提出的，REST是一种针对网络应用的设计和开发方式，是一种风格，可以降低开发的复杂性，提高系统的可伸缩性。<br />
REST强调如下的体系架构概念。<br />
　　1、网络上的所有事物都被抽象为资源（resource）；<br />
　　2、每个资源对应一个唯一的资源标识（resource identifier），<br />
　　3、通过通用的连接器接口（generic connector interface）对资源进行操作；<br />
　　4、对资源的各种操作不会改变资源标识；<br />
     5、所有的操作都是无状态的（stateless）。</p>
<p>REST对于信息的核心抽象是资源，一个资源是一组实体的概念上的映射，而REST使用一个资源标识符来标识组件之间交互所涉及到的特定资源。REST连接器提供了访问和操作资源的值集合的一个通用接口。在这里所有的操作中，它们都是无状态的，这样就不必在多个请求之间保存状态，不必考虑上下文的约束，从而允许服务器组件迅速释放资源，并进一步简化其实现，从而提高系统的可伸缩性。</p>
<p>现在我们就一个简单的示例演示下REST。<br />
在我们的示例中以Javascript为客户端，与HTTP服务器体系架构配合工作，使用URL作为资源标识，并将HTTP作为连接器接口。<br />
客户端的代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #000066; font-weight: bold;">function</span> rest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">var</span> XMLHttpFactories <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
                    <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Msxml2.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Msxml3.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">var</span> xmlhttp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> XMLHttpFactories.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                        xmlhttp <span style="color: #339933;">=</span> XMLHttpFactories<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #006600; font-style: italic;">// 建立XMLHttpRequest对象</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">xmlhttp</span> <span style="color: #339933;">=</span> xmlhttp<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            rest.<span style="color: #000066; font-weight: bold;">prototype</span>._get <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">var</span> xmlhttp <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">xmlhttp</span><span style="color: #339933;">;</span>
                xmlhttp.<span style="color: #660066;">open</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> url <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&amp;&quot;</span> <span style="color: #339933;">+</span> data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                xmlhttp.<span style="color: #660066;">send</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> xmlhttp.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            rest.<span style="color: #000066; font-weight: bold;">prototype</span>._post <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>  data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">var</span> xmlhttp <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">xmlhttp</span><span style="color: #339933;">;</span>
                xmlhttp.<span style="color: #660066;">open</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                xmlhttp.<span style="color: #660066;">setRequestHeader</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-Type&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;application/x-www-form-urlencoded; charset=UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                xmlhttp.<span style="color: #660066;">setRequestHeader</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-Length&quot;</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                xmlhttp.<span style="color: #660066;">send</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> xmlhttp.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            rest.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #000066; font-weight: bold;">get</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                url <span style="color: #339933;">=</span> url <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;?op=GET&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._get<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            rest.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">post</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>  data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                url <span style="color: #339933;">=</span> url <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;?op=POST&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._post<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            rest.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">put</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                url <span style="color: #339933;">=</span> url <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;?op=PUT&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._post<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            rest.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">del</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                url <span style="color: #339933;">=</span> url <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;?op=DELETE&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._get<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">function</span> t<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">var</span> restobj <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> rest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                document.<span style="color: #660066;">write</span> <span style="color: #009900;">&#40;</span>restobj.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://localhost/test/service.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;content=GET Content&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                document.<span style="color: #660066;">write</span> <span style="color: #009900;">&#40;</span>restobj.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://localhost/test/service.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;content=POST Content&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                document.<span style="color: #660066;">write</span> <span style="color: #009900;">&#40;</span>restobj.<span style="color: #660066;">put</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://localhost/test/service.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;content=PUT Content&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                document.<span style="color: #660066;">write</span> <span style="color: #009900;">&#40;</span>restobj.<span style="color: #660066;">del</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://localhost/test/service.php&quot;</span><span style="color: #339933;">,</span>  <span style="color: #3366CC;">&quot;content=DELETE Content&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            t<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>如上所示，我们在客户端创建XMLHttpRequest对象，并且通过这个对象实现通过HTTP对服务器的操作GET、PUT、POST和DELETE以检索和修改资源。HTTP则把对每一个资源的操作都限制在了4个之内：GET、POST、PUT和DELETE。HTTP的这四个方法分别对应我们常见的CRUD操作，具体对应关系如下 ：<br />
C(Create)   		<==>   POST<br />
R(Read/Retrieve)          <==>   GET<br />
U(Update) 		        <==>   PUT<br />
D(Delete/Destroy)        <==>   DELETE<br />
虽然HTTP提供了这4个方法，但是在某些情况下，PUT和DELETE方法是不可用的，于是我们在这里使用GET方法和POST方法进行替代。另外，在JS操作时，需要注意同源策略（Same Origin Policy，SOP），考虑跨域的问题。<br />
服务端代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * REST后台程序简单示例
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Resource <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'content='</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;; get resource Successful&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'content='</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;; post resource Successful&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> put<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'content='</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;; update resource Successful&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'content='</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;; delete resource Successful&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$op</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'op'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$op</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$op</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ops</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'get'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'post'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'put'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'delete'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ops</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$op</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'input error!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Resource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$resource</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$op</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>如上所示，是我们提供REST数据的服务器端的代码。这里只是简单的应答请求，输出标识内容。<br />
以上就是我们这个示例的全部了，在这个示例中，我们的整体架构是基于最简单的客户端/服务器模式，客户端使用Javascript，以Ajax实现。我们不需要使用PHP之类的语言生成客户端的页面，所有的客户端的工作都交给Javascript去做,包括从服务器端读取数据，生成页面内容，页面布局等等。在服务器端，我们需要做的是提供资源，针对客户端的请求返回对应的资源，此时的服务器是无状态的。客户与服务器之间的数据交换不依赖于服务器，由客户端来维护状态。 </p>
<p>另外：<strong>REST只是一种体系架构风格，它并没有改变服务器，它改变的是我们的编码风格。</strong></p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/10/ajax-rest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>执行ajax加载的页面中包含的javascript</title>
		<link>https://www.phppan.com/2009/11/ajax-javascript-onload/</link>
		<comments>https://www.phppan.com/2009/11/ajax-javascript-onload/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 04:05:39 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[程序相关]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JAVASCRIPT]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=387</guid>
		<description><![CDATA[执行ajax加载的页面中包含的javascript 1、【使用iframe】 在需要加载的页面中添加一个ifr [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>执行ajax加载的页面中包含的javascript</p>
<p>1、【使用iframe】<br />
在需要加载的页面中添加一个iframe，如下</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;iframe style=&quot;display:none&quot; onload=&quot;javascript: close_ticket_onload()&quot;&gt;&lt;/iframe&gt;</pre></td></tr></table></div>

<p>onload中添加你所要调用的函数，如果加载的页面在弹出层中，此函数需要放在弹出此层的父页面！</p>
<p>2、【使用eval】<br />
如果使用innerHTML填充XMLHttp取得的数据。如：xxx.innerHTML=XMLHttp取得的数据<br />
这样是不会执行JS，添加eval方法就ok了：如：xxx.innerHTML=eval(返XMLHttp取得的数据);<br />
其它细节请移步：http://blog.zol.com.cn/881/article_880154.html</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/11/ajax-javascript-onload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax总结</title>
		<link>https://www.phppan.com/2009/09/ajax-summary/</link>
		<comments>https://www.phppan.com/2009/09/ajax-summary/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 07:08:35 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[程序相关]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Ajax原理]]></category>
		<category><![CDATA[Ajax实例]]></category>
		<category><![CDATA[Ajax的优点]]></category>
		<category><![CDATA[Ajax的名称]]></category>
		<category><![CDATA[Ajax的缺点]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=237</guid>
		<description><![CDATA[在工作中用了Ajax N多次了，也看过一些相关方面的书籍，也算是认识了它，但是一直没有认真总结和整理过相关的东 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>在工作中用了Ajax N多次了，也看过一些相关方面的书籍，也算是认识了它，但是一直没有认真总结和整理过相关的东东，失败！<br />
近有闲情，将之总结如下：</p>
<p>【名称】<br />
Ajax是Asynchronous JavaScript and XML(以及 DHTML 等)的缩写。<br />
详情请移步Ajax: A New Approach to Web Applications</p>
<p>【原理】<br />
简单一些，就是通过使用XmlHttpRequest对象向服务器发送异步请求，获取返回的数据，并使用Javascript和DOM操作页面内的元素，从而达到改变页面内容的目的。<br />
其中XmlHttpRequest对象是关键，因为它支持异步请求。XMLHttpRequest是完全用来向服务器发出一个请求的。它所包含的方法和属性如下所示：<br />
方法：<br />
●abort() 导致当前正在请求被取消<br />
●getAllResponseHeaders() 返回一个字符串，包含氖 响应标头的名称和值<br />
●getResponseHeader(name) 返回指定的响应标头的值<br />
●open(method, url, async, username, password) 设置请求的方法和目标URL。请求可以声明为同步的（可选），也可以给需要基于窗口谁的请求而提供用户名和口令（可选）<br />
●send(content) 发起带有指定内容（可选）的请求<br />
●setRequestHeader(name, value) 利用指定的名称和值，设置一个请求标头</p>
<p>属性：<br />
●onreadystatechange 指派在请求的状态发生变化时所使用的事件处理程序<br />
●readyState 一个整数值，指示请求的状态如下：<br />
0&#8212;-未初始化<br />
1&#8212;-正在加载<br />
2&#8212;-已加载<br />
3&#8212;-交互<br />
4&#8212;&#8211;完成<br />
●responseText 在响应里所返回的内容<br />
●responseXML 如果内容是XML，就根据内容而创建XML DOM<br />
●status 从服务器所返回的响应状态码。例如：200表示成功，404表示未找到，参考HTTP规范<br />
●statusText 响应所返回的状态文本消息</p>
<p>对于此对象其它介绍请移步：<a href="http://ajava.org/readbook/AJAX/ria/10571.html">XMLHttpRequest概述</a></p>
<p>【所包含的技术】<br />
·  基于XHTML和CSS标准的表示；<br />
·  使用Document Object Model进行动态显示和交互；<br />
·  使用XMLHttpRequest与服务器进行异步通信；<br />
·  使用JavaScript绑定一切；<br />
·  使用XML和XSLT;交换和操作数据。<br />
以上的技术都是一些广泛使用了的技术，都属于比较旧的技术，ajax是这几种技术的结合体。</p>
<p>【简单实例】</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="html" style="font-family:monospace;">&nbsp;
    &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;/title&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;script type=&quot;text/javascript&quot;&gt;
            function ajax() {
                var xmlHttp;
				//	 依据对象判断，而不是依据浏览器
                if(window.XMLHttpRequest)                {
                    xmlHttp=new XMLHttpRequest();//mozilla浏览器
                }else if(window.ActiveXObject){
                    try{
                        xmlHttp=new ActiveXObject(&quot;Msxmlx2.XMLHTTP&quot;);   //IE老版本
                    }catch(e){}
                    try{
                        xmlHttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;); //IE新版本
                    }catch(e){}
&nbsp;
                    if(!xmlHttp){
                        window.alert(&quot;不能创建XMLHttpRequest对象实列&quot;);
                        return false;
                    }
                }
&nbsp;
                if (!xmlHttp) {
                    alert(&quot;创建XMLHttpRequest对象失败！&quot;);
                    return false;
                }
&nbsp;
                xmlHttp.open('POST', 'index.php?get_a=2&amp;get_b=3', false);
     xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;');
                xmlHttp.send(&quot;post_a=1&amp;post_b=2&quot;);
                xmlHttp.onreadystatechange = function() {
                    alert(xmlHttp.readyState);
                }
                if(xmlHttp.readyState == 4){    //判断对象状态
                    var content_obj = document.getElementById(&quot;content&quot;);
                    content_obj.innerHTML = &quot;正在处理数据...&quot;;
                    if(xmlHttp.status == 200){  //信息已经成功返回，开始处理信息
                        var returnStr = xmlHttp.responseText;
                        content_obj.innerHTML =  returnStr;
                    }else{  //页面不正常
                        content_obj.innerHTML = &quot;您所请求的页面存在异常！&quot;;
                    }
                }
            }
&nbsp;
&nbsp;
        &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;input type=&quot;button&quot; value=&quot;ajax&quot; onclick=&quot;ajax();&quot; /&gt;
        &lt;div id=&quot;content&quot;&gt;ajax内容显示区&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>切记：当发起一个POST请求时，需要对报头 Content-type（内容类型）进行设置。这样，服务器就知道如何来处理上传的内容。如果要模拟通过HTTP协议的POST方式来发送表单，则应将内容类型设置为application/x-www-form-urlencoded。</p>
<p>【优点】</p>
<p>●页面无刷新，用户体验好；<br />
●异步，不打断用户操作，响应速度快；<br />
●“按需取数据”，减少冗余请求，减轻服务器负担；<br />
●基于标准化的并被广泛支持的技术，无需额外的插件；<br />
●可以使数据和表现分离；</p>
<p>【存在的问题】<br />
●一些设备还不支持<br />
●开发成本提高<br />
●使back按钮失效，用户操作后无法返回；<br />
●对流媒体支持没有flash之流好；<br />
●对搜索引擎不友好<br />
●破坏程序的异常机制<br />
●存在一些安全问题，暴露了一些程序接口和数据逻辑</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/09/ajax-summary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
