简体   繁体   English

处理AJAX请求的最有效方法是什么?

[英]What is the most efficient way to process AJAX requests?

For example let's say I have a controller called News. 例如,假设我有一个名为News的控制器。 Methods of News include Create, Edit, Delete, etc. So let's say for users with javascript enabled, we can use AJAX to deal with these things and use the controllers if not. 新闻的方法包括创建,编辑,删除等。因此,对于启用了javascript的用户来说,我们可以使用AJAX处理这些事情,如果没有,可以使用控制器。 For example, going to /news we can create news, edit, or delete without leaving this page...with AJAX. 例如,转到/ news,我们可以创建新闻,编辑或删除,而无需离开此页面...使用AJAX。 Without javascript, we would have to go to /news/create, /news/edit/1, /news/delete/1, etc. 没有javascript,我们将不得不转到/ news / create,/ news / edit / 1,/ news / delete / 1等。

So then what way is more efficient to deal with AJAX requests? 那么,哪种方法更有效地处理AJAX请求呢? On each controller should I just have a conditional to check whether the request was sent with AJAX, something like: 在每个控制器上,我都应该有一个条件来检查请求是否通过AJAX发送,例如:

if(isset($_POST['ajax'])) {
    // serve ajax request
} else {
    // serve regular request
}

Or should I create additional methods such as ajaxCreate, ajaxEdit, ajaxDelete, etc? 还是应该创建其他方法,例如ajaxCreate,ajaxEdit,ajaxDelete等?

Shouldn't your AJAX requests be almost the same as your regular requests, except the regular requests have an additional layer (rendering a page) on top? 您的AJAX请求是否应该与常规请求几乎相同,除了常规请求在顶部还有一个额外的层(呈现页面)之外?

Your conditional check should simply regulate whether you respond with something like a JSON response, or a full page - in effect, what view you invoke (assuming an MVC-style setup). 您的条件检查应仅调节您是使用JSON响应还是整页响应-实际上,您调用的是哪种视图(假设使用MVC样式的设置)。

I prefer to pull this functionality into an API. 我更喜欢将此功能引入API。 For example, I can have a folder structure like the following: 例如,我可以具有如下所示的文件夹结构:

web
  - api
      - 1.0
         - news
            create.php
  news.php

Then your page is accessible at the 'news.php' URL. 然后,可以通过“ news.php” URL访问您的页面。 Inside this file you consume your own api. 在此文件中,您将使用自己的api。 The AJAX would also directly access the API at the 'api/1.0/news/create.php' URL for instance. 例如,AJAX还将直接通过“ api / 1.0 / news / create.php” URL访问API。

This leads to less API code in the controllers/views. 这样可以减少控制器/视图中的API代码。

Bob 短发

Never use /news/delete/1 to delete news! 切勿使用/news/delete/1删除新闻!

use POST /news/delete and pass a news id 使用POST /news/delete并传递新闻ID

With that said, both ajax request and regular requests should be made to the same URL. 话虽如此,ajax请求和常规请求都应该对相同的URL进行。 the difference is that regular requests will return an html response and an ajax request will return xml/json/etc 不同之处在于常规请求将返回html响应,而ajax请求将返回xml / json / etc

Page: /news/create/ 页面: /news/create/

// create new news item code goes here

// most newer javascript libraries set this header so you know the page
// was requested via ajax
if ( isset( $_SERVER['X-Requested-With'] && $_SERVER['X-Requested-With'] == 'XMLHttpRequest' ) {
    //output status code or new news item
    exit;
}

// not an ajax request load a view
require( 'views/news_create.php' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM