我想在ajax进程中向URL添加一些字符串,但是每次我访问该函数都失败。 当我提交表单时,它仅访问此URL'Master_data /'
var jenis_submit = document.getElementById('jenis_submit');
var submit_url = '';
if (jenis_submit == '1') {
submit_url = "func_save_instansi";
} else if (jenis_submit == '2') {
submit_url = "func_update_instansi";
}
$.ajax({
url: '<?php echo base_url();?>Master_data/' + submit_url,
method: 'POST',
data: dataKirim,
contentType: false,
cache: false,
processData: false,
success: function(data) {
// console.log(data);
$('#notif').html(data);
}
});
答案 0 :(得分:0)
只需将php值分配给js变量,并同时设置concat
var url = '<?php echo base_url();?>Master_data/';
然后在ajax
url: url + submit_url,
已更新:
我错过了您的代码中的另一个错误
var number = document.getElementById("number").value; //in your code `.value` is missing
如上述(M.Hemant)的回答中所述。
答案 1 :(得分:0)
首先,像这样通过javascript通过ID获得价值
//var number = document.getElementById("number").value; //in your code `.value` is missing
var jenis_submit = document.getElementById('jenis_submit').value;
var submit_url = '';
if (jenis_submit == '1') {
submit_url = "func_save_instansi";
} else if (jenis_submit == '2') {
submit_url = "func_update_instansi";
}
然后 以此替换您的代码
url: "<?php echo base_url('master_data/'); ?>" + submit_url,
答案 2 :(得分:0)
尝试一下:
var jenis_submit = document.getElementById('jenis_submit');
var submit_url = '';
if (jenis_submit == '1') {
submit_url = "func_save_instansi";
} else if(jenis_submit == '2') {
submit_url = "func_update_instansi";
}
var post_url = '<?php echo base_url('Master_data'); ?>' + '/' + submit_url;
$.ajax({
type: "POST",
url: post_url,
data: dataKirim,
beforeSend: function() {
// you can show a preloader here
}
}).done(function(data) {
// hide the preloader
// console.log(data);
$('#notif').html(data);
}).fail(function() {
// hide the preloader
// show an error message
});