- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在寻找最有效的方法来处理 ajax 请求作为使用普通表单的同步请求。据我所知,有 2 种方法可以处理例如新的订单发布请求:
选项 1: Controller 中的 AJAX 检查(为简单起见,省略了验证和保存)。
//Check if we are handling an ajax call. If it is an ajax call: return response
//If it's a sync request redirect back to the overview
if (Request::ajax()) {
return json_encode($order);
} elseif ($order) {
return Redirect::to('orders/overview');
} else {
return Redirect::to('orders/new')->with_input()->with_errors($validation);
}
在上述情况下,我必须在每个 Controller 中执行此检查。第二种情况解决了问题,但对我来说似乎有些矫枉过正。
选项 2:让路由器处理请求检查并根据请求分配 Controller 。
//Assign a special restful AJAX controller to handle ajax request send by (for example) Backbone. The AJAX controllers always show JSON and the normal controllers always redirect like in the old days.
if (Request::ajax()) {
Route::post('orders', 'ajax.orders@create');
Route::put('orders/(:any)', 'ajax.orders@update');
Route::delete('orders/(:any)', 'ajax.orders@destroy');
} else {
Route::post('orders', 'orders@create');
Route::put('orders/(:any)', 'orders@update');
Route::delete('orders/(:any)', 'orders@destroy');
}
第二个选项在路由方面对我来说似乎更清晰,但它不是在工作负载(处理模型交互等)方面。
解决方案(思想家)
thinkers 的回答是正确的,并为我解决了它。下面是扩展 Controller 类的更多详细信息:
最佳答案
A solution我留在 Laravel 论坛涉及扩展核心 Controller 类来管理基于 REST 的系统的 ajax 和非 ajax 请求。无需检查路由并根据请求传输进行切换,您只需在 Controller 中添加一些函数,并以 'ajax_'
为前缀。因此,例如,您的 Controller 将具有以下功能
public function get_orders() { will return results of non-ajax GET request}
public function ajax_get_orders() { will return results of ajax GET request }
public function post_orders() {will return results of non-ajax POST request }
public function ajax_post_orders() { will return results of ajax POST request }
等等
你可以找到粘贴 here
为了扩展核心 Controller 类,您必须更改 application/config/application.php 中的别名“Controller”类,然后将 Controller 类中的 $ajaxful
属性设置为 true (和 $restful
如果你想要 restuful ajax Controller 也是如此。
关于php - Laravel Restfull Controller 和路由 ajax/sync 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15784812/
我使用 django 作为后端并使用 AngularJS 从我的 RESTfull 端点中提取数据。 我遇到了一个我想不出最佳解决方案的问题。 目前我有这个: /api/planned-meals?f
我在 SpringBoot 中使用 JPA 使用 MySql 数据库创建了一个完整的 Web 服务,我的 Web 服务在 localhost 上运行良好用windows机器。但是当我在 Linux 上
自然而然的答案当然是否定的,所以为了理解我的意思,请让我解释一下。 我们有一个服务,其中客户端 POST 要执行的作业,但它们不会在发布时执行。相反,我们的服务使用一个 ID 进行响应,客户端可以使用
我们正在尝试开发一个 Web 应用程序,其中 Controller (Servlet)应充当静态服务,最初浏览器将充当客户端,然后将来可能会有第三方应用程序使用来自同一 Controller 的服务。
这是我第一次尝试使用 RESTfull 应用程序。我已经阅读了很多与我的问题相同的页面,但他们并没有安静地击中目标。 这是我的服务。 package com.tutorialspoint; impor
我正在尝试使用会出错的安全 Restful 服务 Error = Error Domain=NSURLErrorDomain Code=-1202 “此服务器的证书无效。您可能正在连接到一个伪装成“x
我在 IIS 上发布了简单的 .net restful web 服务: [OperationContract] [WebInvoke(Method = "GET", ResponseF
无论在浏览器中输入什么路径,meteor 应用程序总是返回一个模板。如何添加路由以在 meteor 应用程序中启用 RESTfull 功能? 最佳答案 为了向我的 meteor 应用程序添加 REST
@Path("file.upload") public class UploadFileService { @POST @Consumes(MediaType.MULTIPART_FORM_DATA)
我有以下类和 web.xml 文件。 spring框架是否支持@PATH、@PUT、@Consumes...等jax-rs注解? 换句话说,我可以使用 @PATH 代替 @RequestMapping
我在 .NET 4 中创建了一个 WCF restfull 服务,并在 IIS 7.5/windows 7 x86 下发布了它。动词 GET 和 POST 工作正常,但 DELETE 和 PUT 给出
我的一个 Controller 有问题。当我使用(jason内容类型)从 Angular 发送(POST)数据到java Controller 时,我看到主题中可见的错误。所以我无法捕获服务器的响应。
我正在使用 restfull webservice 开发一个网站。我收到此错误 java.lang.ClassCastException: com.ehr.data.CombineClass cann
我制作了一个简单的 REST Jersey 应用程序,从 maven jersey-quickstart-grizzly 原型(prototype)开始(来自 http://download.java
我已经创建了 WCF Restfull 服务,但是当我从 JS 调用它时,它总是说找不到 Respurce。这是y员工类 namespace WcfRestMVC.Models { [Data
假设我想为一家书店开发一个 RESTfull WebApi。我将有一个 Api 来获取书籍信息,例如:books/1。 我想在有人获得图书信息时创建一个日志。因此,稍后我可以通过 Api 生成关于哪本
我用 Java 开发了一个网络服务。下面是它的一个方法。 @Path("/setup") public class SetupJSONService { @POST @Path("/i
我习惯做ROR,但我需要在Java环境中制作一个RESTfull WebService。所以我决定用 Play 来尝试一下!因为它看起来确实是个好主意。 所以我试图找到一种方法将 JSON 添加到我已
我有一个 ProductsController,我想创建一个复制产品的操作方法。它只需要一个参数,即一个 Id,这让我想要使用一个简单的 GET 请求。不过,有些东西告诉我 GET 在这里没有意义。我
我正在寻找最有效的方法来处理 ajax 请求作为使用普通表单的同步请求。据我所知,有 2 种方法可以处理例如新的订单发布请求: 选项 1: Controller 中的 AJAX 检查(为简单起见,省略
我是一名优秀的程序员,十分优秀!