繁体   English   中英

通过Ajax在URL中传递变量

[英]Pass variable in URL via Ajax

这是一段使用jQuery Datatables的JavaScript代码。

我想通过Ajax在URL中传递变量ident (在下面的示例中等于2 ),请参见下文:

ajax: "staff2.php?userid='+ ident +'"

它没有正确通过。 但是用2代替'+ ident +'

那么那条线怎么了?

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 +'",

ajax: "staff2.php?userid=" + ident,

如果ident不仅包含数字,还需要使用encodeURIComponent()将其编码为encodeURIComponent(ident)以正确转义特殊字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM