user3386779
user3386779

Reputation: 7185

passing multiple data in $.ajax based in if condition

I want to pass 4 parameter for role number 7 and 3 parameter for other role.now getting error in if condition.How to pass multiple data in if condition

Uncaught SyntaxError: Unexpected token ==

$.ajax({
                    url: 'bulkUpdate',
                    type: 'get',
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    if(role==7){
                    data: {
                        'ids'  : join_selected_values,
                        'role' :role,
                        'prices':selected_price,
                        'currency':selected_currency
                    },
                    }
                    else{
                    data: {
                        'ids'  : join_selected_values,
                        'role' :role,
                        'comment':comment
                    },  
                    }
                    success: function (data) {
                    console.log('success');
                    },
                    }); 

Upvotes: 0

Views: 2124

Answers (4)

varad mayee
varad mayee

Reputation: 619

You can do it like below

 var datatosend = { ids: join_selected_values, role: role}
    $.ajax({
        url: 'bulkUpdate',
        type: 'get',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        data: {data:datatosend},
        success: function(data) {
            console.log('success');
        },
    });

You will get all your parameters in server side and do your other procedures as per role.

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You are using if-else in $.ajax object. So, what you can do is that you can compare your condition and then create a data object outside the $.ajax call. Using this will prevent that error.

var role = 10;
var data;
if(role == 7){
      data = {
          'ids'  : join_selected_values,
          'role' :role,
          'prices':selected_price,
          'currency':selected_currency
        }
      }else {
      data = {
          'ids'  : join_selected_values,
          'role' :role,
          'comment':comment
      }
}
$.ajax({
        url: 'bulkUpdate',
        type: 'get',
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        data: data,  
        success: function (data) {
          console.log('success');
        }
}); 

Upvotes: 1

Atul Sharma
Atul Sharma

Reputation: 10720

$.ajax({
  url: 'bulkUpdate',
  type: 'get',
  headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
  
  data : role==7 ? {
                        'ids'  : join_selected_values,
                        'role' :role,
                        'prices':selected_price,
                        'currency':selected_currency
                    } :  {
                        'ids'  : join_selected_values,
                        'role' :role,
                        'comment':comment
                    }, 
  
  success: function (data) {
    console.log('success');
   },


});

Since $.ajax({}) accepts a object if(role==7){ will not work directly.

So either user ternary operator or decide the value prior to calling ajax function.

var data = {};

if(role==7){
 data =  { 'ids'  : join_selected_values,
            'role' :role,
             'prices':selected_price,
             'currency':selected_currency
          },
 }else{
   data =  {'ids'  : join_selected_values,
             'role' :role,
            'comment':comment
          },  
  }

Upvotes: 1

Terry
Terry

Reputation: 66188

That's because you're providing an incorrect syntax. Simply build the data object outside if the AJAX request:

var ajaxData = {
    ids: join_selected_values,
    role: role
};

if (role === 7) {
    ajaxData['prices'] = selected_price;
    ajaxData['currency'] = selected_currency;
} else {
    ajaxData['comment'] = comment;
}

$.ajax({
    url: 'bulkUpdate',
    type: 'get',
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    data: ajaxData,
    success: function(data) {
        console.log('success');
    },
})

Upvotes: 1

Related Questions