Get json through ajax / javascript

2

I have a webservice developed in Java that is responsible for returning all the compositions of a particular fabric / piece (textile area).

you must first obtain the current drawing and then the remaining ones. So far there is no problem.

The part I tried to explain explains belongs to the backend . The problem lies in the frontend , more specifically on the page that is responsible for updating an article. An article can be described as a product, which has already been designed, and / or planned and which is in catalog. Faced with this, as I said earlier, I have to previously show the values of the attributes of an article. Note that the article with ref "UL ..." already has some values filled in:

Theproblemoccurs,asIsaid,themomentItrytogetthecomposition.IshowbelowthecodethatItriedtoimplementandthatiscausingproblems:

functionpopulate(frm,data){varout="";
    for (var key in data) {
if(key == "composicao")
        {
            out=encodeURI(data[key]);

            var uri="http://127.0.0.1:8080/revistaSystem/resplaneamento/ServicoComposicao/ObterTodasAsComposicoes/";
            var urifinal = uri.concat(out);
            alert(urifinal);

                    $.ajax({
                             url:urifinal,
                             complete: function (response) {
                             var data=response.responseText;
                             alert(data);           
                             var jsonData = $.parseJSON(data);

                             var $select = $('#composicao');

                             $select.empty();

                             $(jsonData).each(function (index, o) {    
                                var $option = $("<option/>").attr("value", o.numPK).text(o.composicao);
                                $select.append($option);
                            });

                                //preencher option

                            },
                            error: function () {
                                $('#output').html('Bummer: there was an 1error!');
                            },
                    });
        }   
            }

Faced with this, in the case of a particular composition, there is a need to encode the parameter (the string that enters as a parameter), since a composition, of a given fabric, can be simple or composite , that is:

  • 100% Cotton (single composition)
  • 30% Cotton / 60% Flax (Composite Composition)
  • Because an article can have a composite composition, it is always separated by a "/" . Here's the problem. The webservice always takes a JSON to be interpreted through JavaScript / AJAX, but on the side of the frontend I can not get JSON, I'm just getting the following message error):

    Valuetakenupbywebservice:

    How can I solve this problem?

        
    asked by anonymous 06.06.2018 / 15:57

    1 answer

    0

    The problem was in the coding of the parameter, as I suspected, so I performed the following steps:

    1: I implemented a new function similar to the urlencode () function that exists for php, and that I also used it in this project.

       function urlencode(text) {
            return encodeURIComponent(text).replace(/!/g,  '%21')
                                           .replace(/'/g,  '%27')
                                           .replace(/\(/g, '%28')
                                           .replace(/\)/g, '%29')
                                           .replace(/\*/g, '%2A')
                                           .replace(/%20/g, '+');
        }
    

    2: Given that the variable out gets a value that is used as a parameter, I also made this change:

    out=urlencode(data[key]);
    

    Problem solved:)

        
    06.06.2018 / 17:14