Reputation: 1253
I am trying to show data which is passed from controller to blade and manipulate something with it in my view page. But I can see the my data passed correctly in my console but i am not getting it in my view file. I am using Laravel 4
Controller:
public function addRow()
{
if(Request::ajax()){
$row = Input::all();
}
return View::make('add-stock')
->with('rows', $row);
}
View :
@if( !empty($rows) )
@foreach($rows as $row)
{{ $row[0] }}
@endforeach
@endif
route:
Route::post('add-stock/row','StockController@addRow');
jQuery :
<script>
$(document).ready(function(){
$('.rowForm').click(function(e){
e.preventDefault();
var rowVal = $('input[name=row]').val();
//ajax post
$.post('add-stock/row', {row:rowVal}, function(data){
console.log(data);
})
})
})
</script>
Upvotes: 0
Views: 2460
Reputation: 9853
I would do like this. Your ajax function........
public function addRow(){
$row = Input::all();
return Response::json($row);
}
And then your JQuery............
<script>
$(document).ready(function(){
$('.rowForm').click(function(e){
e.preventDefault();
var rowVal = $('input[name=row]').val();
//ajax post
$.get('/add-stock/row?rowVal =' +rowVal , function(data){
console.log(data);
$('#rowVal ').empty();
$.each(data,function(index,subcatObj)
{
$('#rowVal ').append('<option value="'+subcatObj.value+'">'+subcatObj.value+'</option>');
})
})
})
})
here #rowVal id of the div or select attribute
And atlast ...........the route file
Route::get('add-stock/row','StockController@addRow');
By the way you didn't specify from where JQuery will be called
Upvotes: 1
Reputation: 5958
You can return the response with JSON content type. Luckily, Laravel provides a tool for that. You could do something right this
if(Request::ajax()){
return Response::json(Input::all());
}
for more information: http://laravel.com/docs/4.2/responses#special-responses
Upvotes: 0