Laravel Classroom image

Yasar  Khan / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6|Last
Lessons:-AJAX Content

22.4 JSON Responses
When interacting with Javascript, its useful to be able to return data in JSON format, a string
based array that Javascript is more familiar with.
To return a JSON response, first we need to encode our PHP array, and then send
the appropriate headers to inform the client of our JSON content type.
The above quote used to be true, but the method Response::json() was added with Laravel 3.2
which achieves the same goal.
Let’s create a new route.1 <?php
2
3 // application/routes.php
4 Route::get('content', function() {
5
6 // our data array, soon to be JSON
7 $data = array(
8 'name' => 'Dummy',
9 'size' => 'XL',
10 'color' => 'Blue'
11 );
12
13 return Response::json($data);
14
15 });
Finally we return a response object, passing our data array to the json() method which is
JSON encoded for us. We can optionally pass a different status code other than 200 as a second
parameter.

 

 
 
 

Yasar  Khan

Skills    Laravel

Qualifications :-
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:- Hi i am yasar working as web developer since last 3 years.
Explore
 

  Students (0)