1

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&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
      });

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!

1
  • As I'm sending a GET request, I know i can add the query to the url parameter, but I remember I need to get the term that is being typed in the search box, and I don't mind how to get it in that level... Commented Feb 2, 2017 at 15:31

1 Answer 1

0

I answer myself.

I was reading about the documentation and found and important clue: I can get the ajax "url" property from a javascript function getting as a parameter the term being searched. So I built the url joining the query string and used the encodeURI javascript function and it worked! :-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";);
            },

            ....

So the blank spaces are being translated in this function as %20 and the api is happy and spitting the results...

Hope this helps somebody else. Hugs from Venezuela ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.