L
L
LP-DIMAN2015-12-13 10:05:29
JavaScript
LP-DIMAN, 2015-12-13 10:05:29

Why doesn't Ajax work in Laravel?

There is a js file:

//
// Точка входа.
//

$(document).ready(function(){


$.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
  });
  
  // При нажатии на кнопку id=send
  $('.remember').click(function(){
     var id_advert = $(this).val();
  $(this).html("<button type='checkbox' disabled='disabled'   name='remember' class='remember'>  Запомнили </button>");
  $.get('true_advert',{remember:id_advert});

 	

  });
  /*$('#send').click(function(){
     var params = $('#register').serialize();
  
  $.post('auth/register',params);

 	

  });*/


        
$('.comment').click(function(){
    
  var id_comment = $(this).val();

  console.log(id_comment);
  //$(this).html("<textarea rows=10 cols=70 name='add_comment' class='add_comment' maxlengh='256' autofocus> </textarea><br><button type='submit' name='add' class='add'> Добавить сообщение</button>");
  $.get('edit_advert',{comment:id_comment},function(data,textStatus,jqXHR)
    {
      console.log(data);
      console.log(textStatus);
      console.log(jqXHR);
      
    });
  

});
  
  


 	

  });

So, the first ajax request, where the remember class, works. Everything else is not. I don’t understand what the problem is, I registered the tokens. I just wrote a test ajax:
$('.comment').click(function(){
    
  var id_comment = $(this).val();

  console.log(id_comment);
  //$(this).html("<textarea rows=10 cols=70 name='add_comment' class='add_comment' maxlengh='256' autofocus> </textarea><br><button type='submit' name='add' class='add'> Добавить сообщение</button>");
  $.get('edit_advert',{comment:id_comment},function(data,textStatus,jqXHR)
    {
      console.log(data);
      console.log(textStatus);
      console.log(jqXHR);
      
    });
  

});

Controller:
public function id_advert(Request $request)
  {
    $data['id_advert'] = $request->input('comment');

    return view('aaa',$data);
    //Adverts::add_comment($comment,$id_client,$id_advert);
  

    
  }

Route
Route::get('edit_advert','[email protected]_advert');

View:
<button type="submit" value="{{$advert->id_realty}}" name="comment" class="comment"> Добавить комментарий</button>

I just wrote for an example, so that at least this one would be transferred to the view.
So, the console gives a completely correct answer:
1
script.js:42 hello
script.js:43 success
script.js:44 Object {readyState: 4, responseText: "hello", status: 200, statusText: "OK"}
Just like the server:
hello
So why didn't the view connect when the answers were correct? I'm already suffering with this ajax which day
I also registered in the views:
<meta name="csrf-token" value="{{ csrf_token() }}">
 <input type="hidden" id="token" value="{{ csrf_token() }}">

I have this file with scripts using different controllers. Could this be the reason for the error? Although it would not be reasonable to write each ajax request in a separate file

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mustafo, 2016-01-08
@mustafo

Maybe the .comment button itself is rendered by Ajax? That is, at the moment of "ready" this button is not present.
In this case, the click event does not fire, because should be written
like this instead:

$('body').on('click', '.comment', function () { ... });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question