I'm working on an app that connects to SAP's service layer and make queries from the url (API REST). I'm using the famouse widget select2 but I'm having a problem. I need to make a query to the api, and its sentence has a space character (" "):
"(startswith(CardCode,'"+params.term+"') or startswith(CardName,'"+params.term+"'))
The spaces are those around the "or" operator.
So This is mi code:
$('#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 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
});
As you can see, I'm sending the query as a parameter joining the term that is being typed in the widget. The problema is that select2 is encoding the query such way that replaces spaces with a plus symbol (+) and not "%20" so the api rest service says it is an illegal character and I can't get my result.
This is the way it looks:
https://service.net:50000/b1s/v1/BusinessPartners?%24filter=(startswith(CardCode%2C%27CLIEN%27)+or+startswith(CardName%2C%27CLIEN%27))&%24orderby=cardName
As you all can see, the spaces around "or" operator are being replaced by plus symbols. I have tested the javascript function "encodeUri()" and "encodeURIComponent()" and it does nothing because i think it is being serialized in the code. I added the contentType, I replaced the spaces manually by "%20" but the result is being encoded and its worse (%2520)...
Can anyone help me. Is there a way to change this type of encoding so the spaces change to "%20" and not the damn "+"??
Thank you all!