Angularjs $http.get(localhost/test) does not work in Chrome

Asked

Viewed 760 times

2

In my controller I have a $http.get(http://www.testesitelocal.com:18888/teste_json) that returns the values of the JSON test. I make this request on my local machine. In the Webview I made in Javafx this works normal, but in Chrome it doesn’t work (in Firefox and in IE the normal request occurs).

Error return:

Xmlhttprequest cannot load http://www.testesitelocal.com:18888/teste_json . No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://www.testesitelocal.com:8000 <http://www.testesitelocal.com/>' is therefore not allowed access.

1 answer

2


I found the answer! Look:

Definition of explicit domains: (1, 2, 3 and 4)

Javascript configuration:

With jQuery/Zepto:

function verificaCodigoTurma(codigo, retorno) {
  try {
    $.support.cors = true;
    $.ajax({
      cache: false,
      timeout: 5000,
      //async: false,
      type: 'POST',
      //crossDomain: true,
      url: 'http://TESTESTE/VERIFICA',
      dataType: 'json',
      contentType: 'application/json; charset=utf-8',
      data: '{"sch_code":"' + codigo + '", "app_id":1}',
      //JSON.stringify(teste),
      success: function (data) {
        retorno(data);
      }
    });
  }
  //Exceção geral da sincronização
  catch (err) {}
}

No jQuery / Vanilla-JS:

function verificaCodigoTurma2(codigo, retorno) {
  var xmlhttp = new XMLHttpRequest();

  var params = '{"sch_code":"' + codigo + '", "app_id":1}';

  xmlhttp.open("POST", "http://www.ETESTEr.com.br/Services/TESTE.svc/TESTE", false);
  xmlhttp.setRequestHeader("Content-type", "application/json");
  xmlhttp.setRequestHeader("Content-length", params.length);
  xmlhttp.setRequestHeader("Connection", "close");
  //Para funcionar, além da configuração abaixo é necessário colocar o seguinte header no servidor:
  //Access-Control-Allow-Credentials: true
  xmlhttp.withCredentials = true;
  xmlhttp.send(params);
  var jsonResponse = JSON.parse(xmlhttp.responseText);
  retorno(jsonResponse);
}

You can still use JSONP, in the case of GET requests.

For CORS to work you must also configure the server:

IIS:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
  <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET, POST" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
</httpProtocol>

Apache:

Header add Access-Control-Allow-Origin "*"

Remember that these settings above allow any website to access data of yours, removing its security. So try to restrict the domains covered by Access-Control-Allow-Origin.

Accredited: Airton Barbosa who succeeded and went after the correct answer and even offered more than one solution

  • Leaving aside the disorganization, something that someone will arrange, it is worth remembering that it is better to tidy the application to work with CORS than to disable the security of Google Chrome.

  • 2

    I proposed an edition, but I didn’t change the code, I just passed it on jsbeautifier. If anyone has a cleaner code please edit.

  • Just now I stopped to see who answered the question.

  • hehehehe It’s just that I found the answer and posted it quickly.... I’m sorry.

  • 1

    The dying boy edited the post and don’t know where my edit proposal went: I had rewritten the codes. Then edit again.

  • Relax! Then I’ll stop by your Minecraft ;) abs server

Show 1 more comment

Browser other questions tagged

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