Advertisement
wpshagor

Ajax Post Example in Laravel

Dec 21st, 2019
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.76 KB | None | 0 0
  1. ### To call ajax in laravel, you have to send csrf token as input. There are two ways to send csrf token into input....
  2.  
  3. (Jquery ajax full documentation link https://api.jquery.com/jQuery.ajax/)
  4.  
  5.  
  6. way-1. First one is to send csrf using meta tage in header area, then call it into jquery. The example is given below.
  7.  
  8. --keep this meta in header area
  9. <meta name="csrf-token" content="{{ csrf_token() }}" />
  10.  
  11. --write this code in jquery code area. then you don't need to do anything more.
  12.    $.ajaxSetup({
  13.    headers: {
  14.        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  15.      }
  16.  });
  17.  
  18.    $.ajax({
  19.      url:  '{{route("ajax_groups")}}',
  20.      dataType: 'json',
  21.      type: 'post',
  22.      data: {category_id: cat_id},
  23.      success:function(response){
  24.        console.log(response);        
  25.      }
  26.    });
  27.  
  28. way-2. Send csrf token with the data. The example is given below...
  29.  
  30.  
  31.     var csrf_token= {{ csrf_token()}}
  32.    $.ajax({
  33.      url:  '{{route("ajax_groups")}}',
  34.      dataType: 'json',
  35.      type: 'post',
  36.      data: {category_id: cat_id, _token: csrf_token },
  37.      success:function(response){
  38.        console.log(response);        
  39.      }
  40.    });
  41.  
  42.  
  43.  
  44. ### What is content-type and datatype in an AJAX request?
  45.  
  46. contentType is the type of data you're sending, so application/json; charset=utf-8 is a common one, as is application/x-www-form-urlencoded; charset=UTF-8, which is the default.
  47.  
  48. dataType is what you're expecting back from the server: json, html, text, etc. jQuery will use this to figure out how to populate the success function's parameter.
  49.  
  50. If you're posting something like:
  51.  
  52. {"name":"John Doe"}
  53. and expecting back:
  54.  
  55. {"success":true}
  56. Then you should have:
  57.  
  58. var data = {"name":"John Doe"}
  59. $.ajax({
  60.    dataType : "json",
  61.    contentType: "application/json; charset=utf-8",
  62.    data : JSON.stringify(data),
  63.    success : function(result) {
  64.        alert(result.success); // result is an object which is created from the returned JSON
  65.    },
  66. });
  67. If you're expecting the following:
  68.  
  69. <div>SUCCESS!!!</div>
  70. Then you should do:
  71.  
  72. var data = {"name":"John Doe"}
  73. $.ajax({
  74.     dataType : "html",
  75.     contentType: "application/json; charset=utf-8",
  76.     data : JSON.stringify(data),
  77.     success : function(result) {
  78.         jQuery("#someContainer").html(result); // result is the HTML text
  79.     },
  80. });
  81. One more - if you want to post:
  82.  
  83. name=John&age=34
  84. Then don't stringify the data, and do:
  85.  
  86. var data = {"name":"John", "age": 34}
  87. $.ajax({
  88.    dataType : "html",
  89.    contentType: "application/x-www-form-urlencoded; charset=UTF-8", // this is the default value, so it's optional
  90.     data : data,
  91.     success : function(result) {
  92.         jQuery("#someContainer").html(result); // result is the HTML text
  93.     },
  94. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement