Javascript 通过Ajax在URL中传递变量
这是一段使用jQuery数据表的JavaScript代码 我想通过Ajax在URL中传递变量Javascript 通过Ajax在URL中传递变量,javascript,php,datatables,Javascript,Php,Datatables,这是一段使用jQuery数据表的JavaScript代码 我想通过Ajax在URL中传递变量ident(在下面的示例中它等于2),请参见以下内容: ajax: "staff2.php?userid='+ ident +'" 它没有正常通过。但是,将'+ident+'替换为2是可行的 那么,这条线有什么问题 var ident = '2'; var editor; // use a global for the submit and return data rendering in the
ident
(在下面的示例中它等于2
),请参见以下内容:
ajax: "staff2.php?userid='+ ident +'"
它没有正常通过。但是,将'+ident+'
替换为2
是可行的
那么,这条线有什么问题
var ident = '2';
var editor; // use a global for the submit and return data rendering in the examples
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: "staff2.php?userid='+ ident +'",
table: "#building",
"bProcessing": true,
"bServerSide": true,
fields: [ {
label: "",
name: "building"
}
]
} );
// Activate an inline edit on click of a table cell
$('#building').on( 'click', 'tbody td', function () {
editor.inline( this );
} );
$('#building').DataTable( {
//dom: "Tfrtip",
"searching": false,
"bInfo" : false,
"bPaginate": false,
"bSort": false,
"bVisible": false,
ajax: "staff2.php?userid='+ ident +'",
columns: [
{ data: null, defaultContent: '', orderable: false },
{ data: "building" },
],
order: [ 1, 'asc' ],
tableTools: {
sRowSelect: "os",
sRowSelector: 'td:first-child',
aButtons: [
{ sExtends: "editor_create", editor: editor },
{ sExtends: "editor_edit", editor: editor },
{ sExtends: "editor_remove", editor: editor }
]
}
} );
} );
字符串连接中有一个错误。 替换此行:
ajax: "staff2.php?userid='+ ident +'",
为此:
ajax: "staff2.php?userid=" + ident,
您构建的URL不正确 替换:
ajax: "staff2.php?userid='+ ident +'",
与
如果
ident
不仅包含数字,您还需要将其编码为encodeURIComponent(ident)
,以正确转义特殊字符。如果ident
等于2
,您的代码将生成URLstaff2.php?userid='2'
,这是不正确的。感谢您发现@Gyrocode>。代码作了相应调整。
ajax: "staff2.php?userid=" + ident,