Ajax does not return values on mobile device

Asked

Viewed 74 times

0

beauty? It is the following, I have an ajax method that returns a json, the problem is that this ajax does not run on mobile devices. Always return me fail, however, on pc, everything happens normally.

jQuery.ajax({
            url: url,
            method: "get",
            dataType: "json"
        }).done(function(retorno){
            for(var i = 0; i < retorno.myArrayList.length; i++) {
                if(retorno.myArrayList[i].map.status == 1) {
                    retorno.myArrayList[i].map.status = "Aberta"
                } else if(retorno.myArrayList[i].map.status == 2) {
                    retorno.myArrayList[i].map.status = "Paga"
                } else {
                    retorno.myArrayList[i].map.status = "Cancelada"
                }

                linha += '<tr>';
                linha +=    '<td>' + retorno.myArrayList[i].map.nomePaciente + '</td>';
                linha +=    '<td><a href="'+ retorno.myArrayList[i].map.idFatura + '" data-fatura="' + retorno.myArrayList[i].map.idFatura + '">' + retorno.myArrayList[i].map.nomeFatura + '</a></td>';
                linha +=    '<td>' + retorno.myArrayList[i].map.valorFatura.toLocaleString('pt-br',{style: 'currency', currency: 'BRL'}) + '</td>';
                linha +=    '<td>' + retorno.myArrayList[i].map.vencimento + '</td>';
                linha +=    '<td>' + retorno.myArrayList[i].map.dataPagamento + '</td>';
                linha +=    '<td>' + retorno.myArrayList[i].map.status + '</td>';
                linha += '</tr>';

                totalRec += retorno.myArrayList[i].map.valorFatura;
            }

            table.append(linha);
            retorno = "";
            $(".tr-modal").text(totalRec.toLocaleString('pt-br',{style: 'currency', currency: 'BRL'}))
            $(".modalConta-back").fadeIn();
            $(".modalConta").css("transform", "translateX(0)");

        }).fail(function(erro){
            alert(erro);
        });

The request always fails but on the pc everything happens normally. fail even returns an object but I can’t manipulate or verify the error or pq doesn’t generate json mobile.

I URL through jquery when clicking on table row.

var dia = ($(this).find("td:nth-child(1)").text() < 10) ? "0" + $(this).find("td:nth-child(1)").text() : $(this).find("td:nth-child(1)").text();
var mes = $("#month").val();
var ano = $("#year").val();
var url = http://localhost:8080/meusite/api/findAllContatoFaturasJson.do?day="+ dia + "&month=" + mes + "&year=" + ano +"&tipo=2&p1=0&dentistaId=&vendedorId=&formaPagamentoId=";
  • "does not generate json on mobile." ? Have you tested alert(JSON.stringify(erro));? Or create a div to log like this and use logDiv.textContent = JSON.stringify(erro);

  • Is fairly vague, on mobile is running via HTTP or is it a webView Asset? What is the fail error message? Give details.

  • Is the URL the same in both environments? Mobile can reach the URL?

  • Yes, the url is formed via javascript. I attached the tbm code to see. Then, when clicking on the line, the url is generated, attached to the ajax and returned to json. Or it should be like this on cellular tbm. By json stringy recommended by Sergio, gives the following message: {"readyState":0,"status":0,"statusText":"error"}

  • localhost will not work on mobile, you will need to put the IP of your computer so that it can locate the data

  • or place the url without the http://localhost:8080

  • Daniel, exactly that. I inserted the ip of my device and it worked right on the mobile. valeu ae!!!!

Show 2 more comments

1 answer

0

The problem is in your URL, the host is set to localhost, and the port 8080, as it is running on the mobile the browser will not be able to find the information. You can remove the http://localhost:8080 url, so it does not try to access by this address, but rather by the relative path.

For example, if the address you typed on your phone to access the url was http://192.168.0.100/index, defining without the http://localhost:8080 he will try to locate by http://192.168.0.100/meusite/api/findAllContatoFaturasJson.do...

  • 1

    Daniel, try to do as you suggested, but then the Apache can’t process the requisition. Anyway I will upload to the server, and see how this request will behave on mobile. Post the details later.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.