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.
– Gustavo Rodrigues
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.
– Gustavo Rodrigues
Just now I stopped to see who answered the question.
– Gustavo Rodrigues
hehehehe It’s just that I found the answer and posted it quickly.... I’m sorry.
– peterq
The dying boy edited the post and don’t know where my edit proposal went: I had rewritten the codes. Then edit again.
– Gustavo Rodrigues
Relax! Then I’ll stop by your Minecraft ;) abs server
– peterq