this is my controller:
public function index(Request $request)
{
$items = Post::latest()->paginate(5);
$cmnt = Comment::all();
return response()->json(array('posts'=>$items,'comment'=>$cmnt));
}
this my ajax request
function getPageData() {
$.ajax({
dataType: 'json',
url: "{{route('post_status.index')}}",
data: {page:1}
}).done(function(data){
manageRow(data.data);
});
}
function manageRow(data) {
console.log(data.comment);
}
why i am getting error ??
help me out of this plzz
laravel returns json by default if it doesn't return view, in your case index() should return:
return ['posts' => $items,'comment' => $cmnt];
also I don't think this is correct
{{route('post_status.index')}}
probably should be
{{ url('post_status/index') }}
You can use the type of response from the server.
function getPageData() {
$.ajax({
dataType: 'json',
url: "{{route('post_status.index')}}",
type: 'GET'
data: {page:1}
}).done(function(data){
manageRow(data.data);
});
}
function manageRow(data) {
console.log(data.comment);
}
Related
I have made a AJAX request and trying to show the result as HTML.
My controller:
public function searchByRange(Request $request)
{
$query = DB::table('visitors')
->where('id','=',$request->id)
->where('visitors.user_id','=',Auth::user()->id)
->whereBetween('visitors.created_at', array($request->first_date, $request->second_date) );
$visitors = $query->get();
return view('analytics.analytics-range',['visitors' => $visitors]);
}
My AJAX part:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "post",
url: "{{url('searchByRange')}}",
dataType: 'html',
data:
{
first_date : first_date,
second_date : second_date,
id : id
},
success: function(html)
{
$(".date-range").html(html)
}
});
But the problem is ajax response in console is
ErrorException (E_ERROR) Undefined variable: visitors
What could be possible error for that?
try changing your controller function to:
public function searchByRange(Request $request)
{
$visitors = DB::table('visitors')
->where('id','=',$request->id)
->where('visitors.user_id','=',Auth::user()->id)
->whereBetween('visitors.created_at', array($request->first_date, $request->second_date))
->get();
return view('analytics.analytics-range',['visitors' => $visitors]);
}
I have a problem.
I have an ajax call that have two data to send, one is a form serialized the other is a other variable, i don't know how to use the variables into the first data of ajax call ( the form serialized ) into PHP, how can i do this?
JS:
$.ajax
({
url: "/update",
type: "post",
data:
{
form: $("#formTeamLeaderProduzione").serialize(),
type: "TeamLeaderProduzione"
},
success: function (data)
{
alert(JSON.stringify(data))
},
error: function (msg)
{
alert(JSON.stringify(msg));
}
});
PHP:
Route::post('/update', function (\Illuminate\Http\Request $request)
{
$value = \Illuminate\Support\Facades\Input::get('form');
return response()->json($request->form->inputModificaNomeTeamLeaderProduzione1,200);
});
This is my anwser
Route::post('/update', function (\Illuminate\Http\Request $request)
{
parse_str($request->input('form'), $form);
return response()->json($form['inputModificaNomeTeamLeaderProduzione1'],200);
});
From View Page I am calling Ajax to my Controller, from my Controller I want to create a html data so I am passing my array to View file to create a HTML data.
I don't know why when I try to do console nothing gets return, can anyone tell me where I am going wrong
CallingAjaxFromViewFile
function AjaxgetCheckInChekOutUser(status){
var url = "{{ url('admin/events/ajaxData') }}";
var token = $('#token').val();
if(status){
$.ajax({
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
url: url,
type: 'POST',
data: {'status': status,'_token':token},
success: function (data) {
alert(data);
data = JSON.parse(data);
console.log(data.html);
$('#gymimages').html(data.html);
}
});
}
}
myControllerFile
public function AjaxCurrentPastFutureData($data, $status){
$title = "HDTuto.com";
$html = view('admin.events.ajaxdataview')->with(compact('title'))->render();
//pr($html);
$response = response()->json(['success' => true, 'html' => $html]);
return $response;
}
NowmyViewFile From Where I append HTML Data
<h1><i>{{ $title }} </i></h1>
Can anyone tell me where I am going wrong?
** Use this**
function AjaxgetCheckInChekOutUser(status){
var url = "{{ url('admin/events/ajaxData') }}";
var token = $('#token').val();
if(status){
$.ajax({
url: url,
type: 'POST',
data: {'status': status,'_token':token},
success: function (data) {
$('#gymimages').html(data.html);
}
});
Also update your Controller function
public function AjaxCurrentPastFutureData(Request $request){
$title = "HDTuto.com";
$html = view('my')->with(compact('title'))->render();
//pr($html);
$response = response()->json(['success' => true, 'html' => $html]);
return $response;
}
I have two routes defined like this :
Route::get('directeur/qualite', 'Directeur\qualiteController#index')->name('qualite');
Route::get('directeur/qualite/{texte}','Directeur\qualiteController#getResultOfQuestion')->name('getResultQuestion');
My Controller have this function :
public function getResultOfQuestion(){
$texte=Input::get('texte');
$data = DB::table('questions')->where('texte','=',$texte)->value('code');
return['data'=>$data];
}
And I'm doing a request using Ajax like this :
$.ajax({
type: 'GET',
url: '/emagine/projet1613/public/directeur/qualite/',
data: {
texte: encodeURIComponent(str)
},
success: function (data) {
console.log(data);
},
error: function () {
alert('La requête n\'a pas abouti');
}
});
I would like to get the result of the function defined in the Controller but I can not do it. What am I doing wrong?
Just try this
Controller
public function getResultOfQuestion($texte){
$data = DB::table('questions')->where('texte','=',$texte)->value('code');
return response()->json(array('data' => $data));
}
AJAX request
$.ajax({
type: 'GET',
url: '/emagine/projet1613/public/directeur/qualite/'+encodeURIComponent(str),
success: function (data) {
console.log(data);
},
error: function () {
alert('La requête n\'a pas abouti');
}
});
You should return response:
return response()->json(compact('data'));
I have ajax call from view to route
this is my view:
$.ajax({
url: "{{URL::to('Update_toggle')}}",
type: "POST",
data: {
'id' : id ,
'status' : val,
'case_id': caseID
},
dataType: 'json',
success: function(result){
alert("Details Saved" +result.t);
}
});
this is my controller which works fine:
public function Update_toggle(Request $request)
{
$case_id=$request->case_id;
$id=$request->id;
$status=$request->status;
$toggle=tblClientRequest::where('case_id', 1)
->update([$request->id => $request->status]);
dd($toggle);
}
this is my controller which does not work:
public function Update_toggle(Request $request)
{
$case_id=$request->case_id;
$id=$request->id;
$status=$request->status;
$toggle=tblClientRequest::where('case_id', $case_id)
->update([$request->id => $request->status]);
dd($toggle);
}
this is what it returns in console.log:
0
when I give the number in where function it works fine but when i give the post variable to it does not update the table.
Thanks in advance
ok i have got it
Request $request variable is returning string
so typecast it to integer
$case_id = (int)$request->case_id;
then it worked