Select2 widget send param.term spaces to ajax as "+&quo



我正在开发一个应用程序,该应用程序连接到SAP的服务层,并从url(API REST)进行查询。我正在使用famouse小部件select2,但我遇到了问题。我需要对api进行查询,它的句子有一个空格字符("):

"(startswith(CardCode,'"+params.term+"') or startswith(CardName,'"+params.term+"'))

空格是指"或"运算符周围的空格。

所以这是mi代码:

$('#buscarCliente').select2({
placeholder: "Ingrese código o descripción del cliente",
allowClear: true,
language: "es",
ajax: {
url: SLServer+"BusinessPartners",
crossDomain: true,
xhrFields: {
withCredentials: true
},
dataType: 'json',
type: 'GET',
delay: 550,
params: {
contentType: 'application/raw; charset=utf-8'
},
data: function (params) {
return {
$filter: "(startswith(CardCode,'"+params.term+"') or startswith(CardName,'"+params.term+"'))", // Como se va hacer la busqueda
$orderby : "cardName",
//contentType: 'multipart/form-data;boundary=<Boundary>',
//$filter: "((startswith(CardName,'" + params.term.toUpperCase() + "') or startswith(CardCode,'" + params.term.toUpperCase() + "') or startswith(CardName,'" + params.term + "') or startswith(CardCode,'" + params.term + "')) and CardType eq 'C')&$top=15&$expand=PaymentTermsType",
//$filter: "startswith(CardName,'" + params.term.toUpperCase() + "') or startswith(CardCode,'" + params.term.toUpperCase() + "')",
page: params.page
};
},
processResults: function (data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;

return {
results: $.map(data.value, function(item) {
return { id: item.CardCode, text: "<b>"+item.CardCode+"</b> "+item.CardName }; //adecuamos el arreglo al select
}),
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
/*formatNoMatches: function (term) {
return 'No se encontraron clientes con el código: "' + term + '".<br/><!--span class="link">Click&nbsp;here</span-->',
},*/
minimumInputLength: 1,
//templateResult: formatRepo, // omitted for brevity, see the source of this page
//templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});

正如您所看到的,我将查询作为一个参数发送,该参数加入了在小部件中键入的术语。问题是select2编码查询的方式是用加号(+)代替空格,而不是"%20",所以api rest服务说这是一个非法字符,我无法得到结果。

这就是它的样子:

https://service.net:50000/b1s/v1/BusinessPartners?%24filter=(startswith(CardCode%2C%27CLIEN%27)+or+startswith(CardName%2C%27CLIEN%27))&%24orderby=cardName

正如大家所看到的,"或"运算符周围的空格正被加号所取代。我测试了javascript函数"encodeUri()"one_answers"encodeURIComponent()",但它什么都不做,因为我认为它正在代码中序列化。我添加了contentType,我手动用"%20"替换了空格,但结果是编码的,更糟的是(%2520)。。。

有人能帮我吗?有没有办法改变这种类型的编码,让空格变成"%20",而不是该死的"+"??

谢谢大家!

我自己回答。

我在阅读文档时发现了一条重要的线索:我可以从一个javascript函数中获得ajax"url"属性,该函数将搜索的术语作为参数。因此,我构建了连接查询字符串的url,并使用encodeURI javascript函数,它成功了!:-D

ajax: {
....
url: function (params) {
return SLServer+"BusinessPartners"+encodeURI("?$top=15&$filter=((startswith(CardName,'" + params.term.toUpperCase() + "') or startswith(CardCode,'" + params.term.toUpperCase() + "') or startswith(CardName,'" + params.term + "') or startswith(CardCode,'" + params.term + "')) and CardType eq 'C')&$expand=PaymentTermsType";);
},
....

因此,这个函数中的空格被转换为%20,api很高兴并吐出结果。。。

希望这能帮助其他人。委内瑞拉拥抱;-)

最新更新