class Controller_Test extends Controller_Rest { public function get_list() { $this->response(array( 'foo' => Input::get('foo'), 'baz' => array( 1, 50, 219 ), 'empty' => null )); } }
http://localhost/test/list.json?foo=bar
Samit Rimal wrote on Tuesday 8th of November 2011:Yes i have seen the page . I didnt have how to use it with ajax calls. having a big trouble in using when try to do ajax calls.
class Controller_Test extends \Controller_Rest { function get_testone() { $this->response->set_header('Content-Type','application/json'); return $this->response(array('hello'=>'this is json and it is sucessfull')); } } OUtput : array ( 'hello' => 'this is json and it is sucessfull', ) i need the output: {"hello":"this is json and it is sucessfull"}
<?php namespace Chat; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ class Controller_Test extends \Controller_Rest { function get_testone() { $this->rest_format='json'; $this->response(array('hello'=>'this is json and it is sucessfull')); } function post_test() { $this->rest_format='json'; $this->response(array('hello'=>'this is a post method')); } } ?>filename:modules/chat/views/chat.php
<div id="test1"></div> [removed] var hostname=[removed].pathname; //alert(hostname); $(document).ready(function(){ $("#test").click(function(){ //alert('hello'); $.ajax({ data:{samit:'rimal'}, url:hostname+"/test/testone.php", //when i use /test/testone.json no result is shown in the browser type:"GET", cache: false, success :function(data){ $('#test1').html(data) }, error: function(XMLHttpRequest, textStatus, errorThrown) { $('#test1').html(errorThrown); } }); }); }); [removed]i would like to know if the ajax calls can be done without using rest controller also or not to output in json format. if that would be possible i would use it temporarily ,any answers will be appreciated
controller/ajax.php <?php class Controller_Ajax extends Controller_Rest { public function post_search() { $search = \Input::post("term") . "%"; $result = DB::select()->from('customers')->where_open() ->where('firstname', 'like', $search ) ->where_close() ->or_where_open() ->where('postcode', 'like', $search ) ->or_where('telephone', 'like', $search ) ->or_where_close()->execute(); $data['response'] = 'true'; $data['message'] = array(); foreach ($result as $row) { $data['message'][] = array('label' => $row['firstname'], 'value' => $row['firstname'], 'details' => $row); } $this->response($data); } }
<.Script type="text/javascript"> jQuery(document).ready(function($) { $(function() { $("#query").autocomplete({ minLength: 2, source: function(req, add) { $.ajax({ url: '/ajax/search.json', dataType: 'json', type: 'POST', data: req, success: function(data) { if(data.response =='true') { add(data.message); } } }); }, select: function(event, ui) { var data = ui.item.details; $('#query').val(ui.item.label); $("#title").val(data.title); $("#firstname").val(data.firstname); $("#lastname").val(data.lastname); $("#address1").val(data.address1); $("#address2").val(data.address2); $("#town").val(data.town); $("#county").val(data.county); $("#postcode").val(data.postcode); $("#telephone").val(data.telephone); $("#mobile").val(data.mobile); $("#email").val(data.address1); $("#nationality_id").val(data.nationality_id); $("#dob").val(data.dob); $("#identity_id").val(data.identity_id); $("#notes").val(data.notes); } }); }); }); <.Script>
return array(
/*
| What format should the data be returned in by default?
|
| Default: xml
|
*/
'default_format' => 'json',
class Controller_Api_1_User extends Controller_Rest
{
public function before()
{
parent::before();
if ( ! \Gm::gm_verify_credentials())
{
return $this -> response(array('errors' => 'Unauthorized request.', ), 403);
}
}
public function after($response)
{
return $response;
}
public function get_user_profile()
{
$user = \Gm::gm_profile_info($this->param('screen_name')); //Retrieve a user model
return $this->response(array(
'user' => $user
), 200);
}
}
self.screen_name.subscribe(function(newScreenName) {
self.user([]);
self.pending_request(true);
$.ajax({
url : self.base_api() + 'get/user/' + newScreenName,
type: 'GET',
dataType : 'json',
success : function(d) {
if (d.user)
{
self.user(new ProfileUser(d.user));
}
},
error : function(d) {
}
}).done(function() {
self.pending_request(false);
});
}.bind(self));
It looks like you're new here. If you want to get involved, click one of these buttons!