External connection JSON Webservice

Asked

Viewed 1,083 times

3

I have a PHP webservice feeding data from a array via JSON, for reading in an HTML file.

When I run the application having the $.get with a local file it works, but when I change the path to read the same file on my server, it does not connect.

I noticed on the console that there is an impediment to connections via localhost.

Taking into account that this application will run from the base PhoneGap, what would be the best alternative to address this problem?

4 answers

1

This problem is very common on webservices I suggest you study about CORS http://pt.wikipedia.org/wiki/Cross-origin_resource_sharing.

This is a problem that has to be solved both on the server and in your client application being a JS or other client.

To resolve this on the server you can do as follows, just call this php function in your input file

  function cors() {

// permite para qualquer origem
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: *");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache por 1 dia
}

// Essa parte cuida do preflight request
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {


    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");         

    header("Access-Control-Allow-Headers: Accept, Content-Type");

    exit(0);
}

echo "Você está com CORS!";
}

Remembering that it is not cool for you to access global variables such as $_SERVER directly strongly recommend some php package to do this, if possible a framework or mini framework for this service https://packagist.org/packages/symfony/http-foundation already help.

To solve this on the client side just enable CORS in jquery or at the http of the angle at which you prefer

  $.ajax({
        url: "http://example.com/minha/acao/",
        type: "POST",
        crossDomain: true,
        data: {conteudopost: "algumacoisa"},
        dataType: "json",
        success: function (response) {
          //sucesso
        },
        error: function (xhr, status) {
            //erro
        }
    });

Basically that’s any doubt leaves a comment there, vlw

1

0

I ended up migrating to Angular.js and adopting the most structured CORS to serve my application.

0

I use ? callback=? to access json webservices outside the local domain. Example below.

<script type="text/javascript>
  $(document).ready(function(){
     $.getJSON("http://dominioexterno.com/caminho-do-get/?callback=?", function(exemplo){

        //o que voce quer fazer.

     });
  });  
</script>

There is also the JSONP option, take a look at this link here: Jquery Cross-Domain ajax call using JSONP

  • Andre thanks for the answer, but I’m already using this method. The same works when the file is local and read a local file, or when it is on the server and read a file on the server. My problem is when the request is made from the local server to the web server.

  • <script type="text/javascript"> $(Document). ready(Function(){ $.get("http://www.tassionoronha.com.br/peixes.php", Function(data){ $("#answer1").html(date.text) }, "json") }); </script> <divid="answer1"></div>

Browser other questions tagged

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