站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
▼
建站程序
开发
服务器
办公软件
开发教程
▼
服务器教程
软件使用教程
运营教程
热门电子书
▼
CSS教程
WordPress教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
热点词条
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress:AJAX in Plugin
”(章节)
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
跳转至:
导航
、
搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
== 运行Ajax 基本知识 == There are three steps in an Ajax request, in general: 一般情况下,Ajax请求,有三个步骤: # The user does something (such as clicking or dragging the mouse), and JavaScript embedded in the HTML of the web page responds by composing a "request" and sending it to a special URL on the web server. Due to security restrictions, the URL the request goes to must be on the same web site that the file containing the JavaScript came from. #用户执行一些操作(如点击或者拖拉鼠标),嵌套在网页HTML中的JavaScript就会创建"请求"并且发送到网页服务器上特别的URL中。出于安全考虑,进入的URL请求必须与包含JavaScript的文件位于同一个站点。 # A script or program on the web server (in WordPress, this will generally be one or more PHP functions) processes the request and sends information back to the browser. #网络服务器上的脚本或者程序(在WordPress中,是一个或者多个PHP函数)处理请求并且将信息发送回浏览器。 # The returned information is displayed using JavaScript. #使用JavaScript显示返回的信息。 Unfortunately, much of Ajax is JavaScript, which runs in the user's web browser, and the different web browsers have not all implemented the Ajax calls and responses in the same manner. So, to make things easier, most Ajax developers choose to use a tested cross-browser library that wraps the particular browser idiosyncracies in a standard class with a documented API. In this article, we'll use one such library, [http://www.twilightuniverse.com/projects/sack/ SACK] (Simple Ajax Code-Kit), which is included in WordPress. We'll need to make sure both the SACK library and the JavaScript functions that compose the Ajax request get included in the HTML head section of the web page where the Ajax request will take place; the sections below will show how to do that for both the administration and viewer-facing sides of WordPress. 不幸地是,大多数Ajax是JavaScript,在用户浏览器中运行,不同的浏览器执行Ajax调用与回应的方式不同。因此,要使得操作更为简单,Ajax开发人员选择使用已经在多个浏览器中测试的,包括了标准级别的特别的浏览器的特点的,已归档的API。在这篇文章中,我们会使用这样的浏览器集, [http://www.twilightuniverse.com/projects/sack/ SACK] (简单的 Ajax Code-Kit),包含在WordPress中。我们需要确定创建Ajax请求的SACK集和JavaScript函数都包含在网页的HTML标头部分,Ajax会在这里产生;下面的部分将会显示怎样为WordPress管理界面和访客界面执行这个操作。 When creating an Ajax request using the SACK library, we'll need to supply the following information; the sections below will fill in the details of what this information should be for the administration side and the viewer-facing side: 使用SACK集创建Ajax请求的时候,我们需要支持以下的信息;下面的部分解释了对于管理界面和访客界面,这个信息分别是什么: * '''Request URL''': The URL on the server that will process the Ajax request. * '''请求URL''':服务器上的URL会处理Ajax请求。 * '''Custom request variables''': SACK allows us to set arbitrary request variables, which are sent via POST or GET to the server. Cookie information can also be sent. * '''自定义请求变数''':SACK使我们设置任意的请求变数,通过POST或者GET发送到服务器。也可以发送Cookie信息。 * '''What to do if there is an error''': a JavaScript function to call if there is an Ajax error. * '''如果只有一个错误,怎么办''':如果有个Ajax错误,调用JavaScript函数。 By default, SACK assumes that the returned information from the server is JavaScript code, which is executed when it comes in (asynchronously). In the examples below, we'll use this default behavior, so the PHP functions that are processing Ajax requests will need to compose their results into JavaScript commands. If you want to do something else with the returned information in your plugin, you might want to visit the [http://www.twilightuniverse.com/projects/sack/ SACK Project Home Page], download the zip file, and read the documentation, because there are definitely other possibilities. 默认情况下,SACK假定从服务器中返回的信息是JavaScript代码,进入的时候(不同时),这个代码就会得到执行。在下面的例子中,我们会使用这个默认操作,这样处理Ajax请求的PHP函数就需要将结果创建为JavaScript命令。如果你希望使用插件中返回的信息执行另外一些操作,你可能想要访问[http://www.twilightuniverse.com/projects/sack/ SACK Project 主页],下载压缩文件,阅读文章,因为的确有其它的可能情况。 One other detail is that the PHP function that processes the Ajax request should use the PHP <tt>die</tt> function to pass back its JavaScript-encoded information. Example: <tt>die("javascript_commands_here")</tt> 另外一个详细信息就是处理Ajax请求的PHP函数必须使用PHP <tt>die</tt>函数,返回JavaScript编码的信息。例如:<tt>die("javascript_commands_here")</tt> With this common Ajax background in mind, the next two sections go through examples of how to use Ajax on the administration screens of WordPress, and on the viewer-facing side of WordPress. The two sections are independent, so you can just read the one that is appropriate for your plugin, if you want. 有个这个普通的Ajax背景知识之后,下面的两个部分,介绍了怎样在WordPress管理界面和访客界面上使用Ajax的例子。这两个部分是各自独立的,因此如果你想的话,可以阅读适合你的插件的其中的一个部分。
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)