jquery - 根据 if 条件在 $.ajax 中传递多个数据
问题描述
我想为角色编号 7 传递 4 个参数,为其他角色传递 3 个参数。现在在 if 条件中出现错误。如何在 if 条件下传递多个数据
未捕获的语法错误:意外的令牌 ==
$.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');
},
});
解决方案
那是因为您提供的语法不正确。data
如果 AJAX 请求,只需在外部构建对象:
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');
},
})
推荐阅读
- django - 按返回重复项的模型 B 对模型 A 进行排序
- javascript - 将 Nuxt.js SSR 应用程序部署到生产环境会出现错误
- sql - 找到第 n 个最高的
- javascript - 如何关闭剑道下拉列表的缓存
- r - c() 函数正在将我的值从字符串更改为数字
- sql-server - 带有 GUID 全零的 MS SQL 表
- reactjs - 误报 eslint(no-unused-vars)
- javascript - 在调用另一个函数之前等待父状态更新
- installation - Tidal Cycles cabal 安装失败
- node.js - 在承诺链中调用 process.exit() 的正确位置