【问题标题】:How to pass URI having parameter values with space? in jqgrid如何传递具有空格的参数值的URI?在 jqgrid 中
【发布时间】:2014-12-03 12:01:43
【问题描述】:

主网格代码如下

$(document).ready(function () {
    jQuery("#list5").jqGrid({
        url: 'server.php?mode=getBaseList',
        datatype: "json",
        colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'],
        colModel: [{
            name: 'id',
            index: 'id',
            width: 55
        }, {
            name: 'invdate',
            index: 'invdate',
            width: 90
        }, {
            name: 'name',
            index: 'name',
            width: 100
        }, {
            name: 'amount',
            index: 'amount',
            width: 80,
            align: "right"
        }, {
            name: 'tax',
            index: 'tax',
            width: 80,
            align: "right"
        }, {
            name: 'total',
            index: 'total',
            width: 80,
            align: "right"
        }, {
            name: 'note',
            index: 'note',
            width: 150,
            sortable: false
        }],
        rowNum: 10,
        jsonReader{
         id:'id',repeatitems:false
        },
        rowList: [10, 20, 30],
        pager: '#pager5',
        sortname: 'name',
        autoencode: true,
        loadonce:true,
        sortable: true,
        viewrecords: true,
        sortorder: "desc",
        multiselect: false,
        subGrid: true,
        subGridRowExpanded: function(subgrid_id, row_id) { 
        var newID = $('#list5').getCell(row_id, 'id');
        var escapeID=escape(newID);

        var subgrid_table_id, pager_id; 
        subgrid_table_id = subgrid_id+"_t"; 
        pager_id = "p_"+subgrid_table_id; 
        $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>"); 
        jQuery("#"+subgrid_table_id).jqGrid({ 
        url:"/portal/getSubGridData?id="+escapeID, 
        datatype: "json", 
        colNames: ['No','Item','Qty','Unit','Line Total'], 
        colModel: [ 
        {name:"num",index:"num",width:80,key:true}, 
        {name:"item",index:"item",width:130}
        ], 
        rowNum:20, 
        pager: pager_id, 
        sortname: 'num', 
        sortorder: "asc", 
        height: '100%' 
        }); 
        jQuery("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false}) }
        caption: "Simple data manipulation"
    }).navGrid("#pager5", {
        edit: false,
        add: false,
        del: false
    });
});
jQuery("#list5").jqGrid('filterToolbar',{searchOperators : true});

如何传递带有空格的参数值的URI?我观察到 '%20' 或 '_' 在 JS 中被取消了。我该怎么办?

我试过了: 第 1 步:使用 id

id=10054898 104143018

var modifiedUrl=escape(id);

新网址如下

我的网址在下面

newUrl=/portal/getSubGridData?id=10054898%20104143018

第 2 步:使用名称

name=John Williams

var modifiedUrl=escape(name);

新网址如下

我的网址是

newUrl=/portal/getSubGridData?name=John%20Williams

上面的网址没有发布到服务器端,我已经检查了萤火虫。网址未提交。

【问题讨论】:

  • 尝试发出 POST 请求时遇到什么错误?
  • 您使用的 JavaScript 代码在哪里?如果您使用postData,则根本不存在该问题。可以使用encodeURIComponent 或使用jQuery 的$.param 方法。如果您发布您使用的 JavaScript 代码,我可以通过相应的示例向您发布我的答案。
  • 我使用过 post 方法(mtype 是 post)。但服务器端没有得到数据
  • 如果你使用mtype: "POST",为什么你需要将参数附加到URL而不是正文?如何在服务器代码中获取参数信息?您是否忽略所有向您发送 jqGrid 的标准参数?顺便说一句,您真的在主网格中使用"10054898 104143018" 之类的ID 还是使用"10054898_104143018"
  • 我想将以下 URL 发送到服务器端:/portal/getSubGridData?id=10054898 0104143018。(即带空格的参数)。 EncodeURL/escape 不起作用。请指导我在 url 中发送带有空格的数据

标签: jquery jqgrid jqgrid-php


【解决方案1】:

我假设您使用 Subgrid as Grid 并使用如下代码创建子网格

subGridRowExpanded: function(subgridId, rowId) {
    var id = $.jgrid.stripPref(this.p.idPrefix, rowId);
    ...
    $subgrid.jqGrid({
        url: "/portal/getSubGridData?id=" + id,
    });
    ...
}

您应该从不添加通用字符串值作为 URL 的一部分。而不是你应该使用标准的 JavaScript 方法encodeURIComponent

        url: "/portal/getSubGridData?id=" + encodeURIComponent(id),

或 jQuery.param 方法

        url: "/portal/getSubGridData?" + $.param({id: id}),

使用起来更简单

        url: "/portal/getSubGridData",
        postData: {
            id: id
        }

如果 id 将被添加到将发送到服务器的其他参数中,我希望您在服务器端获取和分析。如果您使用 HTTP GET,那么您在 PHP 中使用 $_GET 访问参数。如果您使用mtype: "POST",那么您可以使用$_POST(类似于$_POST["id"])访问参数。我不是 PHP 开发人员,但我不明白为什么可以将一些参数附加到 URL 并使用标准方式发布另一个参数:在 HTTP POST 请求的主体内部。

最后一句话:我强烈建议您不要在id 中使用空格。取决于您使用的 HTML 版本可能会被禁止。我建议您最好使用下划线而不是空格。

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2017-10-28
    • 2010-12-11
    • 2017-12-09
    相关资源
    最近更新 更多