1

I am trying to send some variables(data),one is the checkbox text, and other is the value in the textarea field to the controller in Laravel through ajax, below is the script:

<script>
    $('#btn1').on('click', function() {

        $('input[type="checkbox"]').on('click', function() {
        var aa=$(this).next('label').text();
        var bb=$('textarea#txt2').val();
        $.ajaxSetup({
            headers:
                {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
        });
           $.ajax({
            type: "POST",
            url: "/masterdata",
            //dataType: 'json',
            data: {aa,bb},
            success:function(){
            console.log(data);    
       }
        ,error:function(){ 
            console.log("Error!!!!");
       }    
    });
    });
    }); 
     </script>

On trying to retrieve the request values in the controller, only the request token gets displayed, and the ajax function doesn't display the success or error message either.What am I missing here?

MVS
  • 108
  • 2
  • 14

4 Answers4

0

Try your data as in this format:

data:{'posa': aa, 'posb': bb},

and

    success:function(data){
        console.log(data);    
   }

Hope this helps.

kshitij
  • 642
  • 9
  • 17
0

you need to set a variable on ajax success like this:

$.ajax({
            type: "POST",
            url: "/masterdata",
            //dataType: 'json',
            data: {aa,bb},
            success:function(response){
            console.log(response);    
       }
        ,error:function(){ 
            console.log("Error!!!!");
       }    
});
Dearwolves
  • 443
  • 4
  • 16
0

i found two issue in your ajax request.

  1. you have to change data: {aa,bb}, to object like data: {aa : aa, bb : bb},
  2. Your success response log is wrong. Current code success:function(), it should be success:function(data)

Full output of new code is :

<script>
    $('#btn1').on('click', function() {

        $('input[type="checkbox"]').on('click', function() {
        var aa=$(this).next('label').text();
        var bb=$('textarea#txt2').val();
        $.ajaxSetup({
            headers:
                {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
        });
           $.ajax({
            type: "POST",
            url: "/masterdata",
            dataType: 'json',
            data: {aa : aa, bb : bb},
            processData: false,
            cache: false, 
            async :false,
            success:function(data){
            console.log(data);    
       }
        ,error:function(){ 
            console.log("Error!!!!");
       }    
    });
    });
    }); 
</script>

To get parameter value in controller use

 /**
 * Store.
 *
 * @param  Request  $request
 * @return Response
 */
public function store(Request $request)
{
    $aa = $request->input('aa');
    $bb = $request->input('bb');
    //Your code here
}
Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46
0
<script>
    $('#btn1').on('click', function() {

        $('input[type="checkbox"]').on('click', function() {
        var aa=$(this).next('label').text();
        var bb=$('textarea#txt2').val();
        $.ajaxSetup({
            headers:
                {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
        });
           $.ajax({
            type: "POST",
            url: "/masterdata",
            //dataType: 'json',
            data: 'aa=test'+'&bb=test',
            success:function(){
            console.log(data);    
       }
        ,error:function(){ 
            console.log("Error!!!!");
       }    
    });
    });
    }); 
     </script>
Priya Goud
  • 95
  • 7