Javascript 通用Ajax请求的RESTful版本

Javascript 通用Ajax请求的RESTful版本,javascript,ajax,json,rest,Javascript,Ajax,Json,Rest,我使用了这种“传统的好方法”通过Ajax获取HTML格式的数据,并将其注入DOM http://localhost/ajax-controller/mobile-view/resource/1/ $mobile_view = new View('mobile-view'); // use mobile view $mobile_view->data = $this->data_array; // add some data to view $this->response->

我使用了这种“传统的好方法”通过Ajax获取HTML格式的数据,并将其注入DOM

http://localhost/ajax-controller/mobile-view/resource/1/

$mobile_view = new View('mobile-view'); // use mobile view
$mobile_view->data = $this->data_array; // add some data to view
$this->response->body($mobile_view);    // return formatted HTML
$web_view = new View('web-view');       // use normal web view
$web_view->data = $this->data_array;    // add some data to view
$this->response->body($web_view);       // return formatted HTML
$this->response->body(json_encode($this->data_array)); // return JSON data
http://localhost/ajax-controller/web-view/resource/1/

$mobile_view = new View('mobile-view'); // use mobile view
$mobile_view->data = $this->data_array; // add some data to view
$this->response->body($mobile_view);    // return formatted HTML
$web_view = new View('web-view');       // use normal web view
$web_view->data = $this->data_array;    // add some data to view
$this->response->body($web_view);       // return formatted HTML
$this->response->body(json_encode($this->data_array)); // return JSON data
问题是什么是RESTful版本

我应该只通过Ajax获取JSON数据吗

http://localhost/ajax-controller/resource/1/

$mobile_view = new View('mobile-view'); // use mobile view
$mobile_view->data = $this->data_array; // add some data to view
$this->response->body($mobile_view);    // return formatted HTML
$web_view = new View('web-view');       // use normal web view
$web_view->data = $this->data_array;    // add some data to view
$this->response->body($web_view);       // return formatted HTML
$this->response->body(json_encode($this->data_array)); // return JSON data

我应该如何处理另一个ajax请求view/HTML格式?还是我遗漏了什么?

在restful服务中有视图是可以的,因为它决定了如何返回数据。我建议传递url参数,如

http://localhost/ajax-controller/resource/1/?view=mobile

要知道如何通过该参数响应,RESTful不仅仅是一个数据,而且可能是最终的HTML版本?RESTful意味着一个定义资源的url。在本例中为“资源/1”。显示该视图的方式不需要是url定义的一部分,因此可以将其设置为url参数。编辑:当然,除非视图实际上影响了资源(这看起来很奇怪)。在这种情况下,它应该是url的一部分,就像在代码中一样。纯粹的REST方法是使用资源url和接受编码头来确定返回数据的格式。