0
I looked at some examples of jQuery documentation, but I haven’t been able to.
Code:
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
var endereco = 'http://10.20.229.17:8085/todosPostos';
$.ajax({
url: endereco,
complete: function(res){
var data = JSON.parse(res.responseText);
console.log(data);
}
});
</script>
</head>
<form action="testeste.html" method="post">
<select>
<option value="trechoorigem">Trecho (Origem)</option>
<option value="trechodestino">Trecho (Destino)</option>
</select>
<input type="text" />
<button type="button">Pesquisar</button>
</form>
</body>
</html>
Console:
Xmlhttprequest cannot load http://10.20.229.17:8085/todosPostos. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.
Uncaught Syntaxerror: Unexpected token u
You are performing a cross Domain call, that is, trying to consume the API (http://10.20.229.17:8085/) from another server (or domain). You need to enable your API to allow Cross-Omain requests.
– Pedro Camara Junior
This answer right here in the OS explains a little about CORS: http://answall.com/questions/3183/requisi%C3%A7%C3%A3o-ajax-cross-Domain-with-javascript-pure-without-Apis#Answer-3251
– Pedro Camara Junior
@Pedrocamarajunior I don’t know if this influences anything, but this API is on a VPN that I’m connected to. Another problem is that I’m not getting JSON either locally.
– Giovanni Bernini
If for example, you are calling on your page (localhost:8080) and your api is at another address (ex localhost:9999) you are already calling cross Omain. I recommend changing the API to accept this type of request. The configuration will depend on the rpogram language you are using in the API.
– Pedro Camara Junior
inside the ajax, place:
crossDomain: true,
, as per this example: http://answall.com/questions/54524/comor-show os-values-storage-no-json-javascript/95061#95061 and in PHP: before json_encode(), put this:header("Access-Control-Allow-Origin: *");
– Ivan Ferrer
@Ivanferrer
$.ajax({
 type: "POST",
 dataType: "json",
 crossDomain: true,
 url: 'http://10.90.226.87:8085/todosPostos',
 complete: function(res){
 var data = JSON.parse(res.responseText);
 console.log(data); 
 }
 });
the date returns: Xmlhttprequest cannot load http://10.90.226.87:8085/allPosts. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.– Giovanni Bernini
I do not know if there is this "complete", I think is "Success". and other, in ajax, do not need to parse, read in the answer of the link I gave you.
– Ivan Ferrer