I know you’ve had your answer, but I’d like to explain how to use the XmlHttpRequest API
.
When you define false
in the third parameter of .open
you are defining as synchronous:
open('POST', url, false);
Even if you use .onload
or .onreadystatechange
they will not work, understand that Ajax is not the technology but the way we use the XmlHttpRequest API
, that is only ajax if it is asynchronous, otherwise it is SJAX (Xmlhttprequest synchronous).
See the differences:
Synchronous (this is not Ajax):
var oReq = new XMLHttpRequest();
//Defina como false
oReq.open("GET", "/url", false);
//Espera completar a requisição, geralmente congela o browser
oReq.send(null);
alert(oReq.responseText);
Note that the synchronous "freezes" the webbrowser while the request does not end
Asynchronous (this is Ajax):
var oReq = new XMLHttpRequest();
//Defina como true
oReq.open("GET", "/url", true);
//Função assíncrona que aguarda a resposta
oReq.onreadystatechange = function()
{
if (oReq.readyState === 4) {
alert(oReq.responseText);
}
};
//Envia a requisição, mas a resposta fica sendo aguardada em Background
oReq.send(null);
Note that the asynchronous does not freeze the browser, because in fact the process is in Background and the signal is sent via callback pro .onreadystatechange
for each stage of the .readyState
It’s as I explained in this question: Ajax is not a programming language. So what is?
application/x-www-form-urlencoded
vs multipart/form-data
I understand what Tobymosque said and I don’t disagree, just that we need to understand the differences, look at the setRequestHeader('Content-Type', ...);
it’s not just something from XmlHttpRequest
as you may already know, it’s an instruction that tells you how data should be interpreted by the back-end.
Use new FormData
works perfectly as explained, but if we don’t have browser support for this class then we have to use application/x-www-form-urlencoded
.
The setRequestHeader('Content-Type', ...);
is equivalent to the attribute enctype
in the <form>
, with them we define how the form data will be encoded when sending them to the server, there are 3 types of values for this attribute:
application/x-www-form-urlencoded
this is the default value in <form>
, but not in the XmlHttpRequest
. In it all characters are encoded before being sent, for example spaces are exchanged for +
and special characters are converted to ASCII HEX values.
multipart/form-data
It does not encode the data, you must use this value when doing uploads.
text/plain
spaces are converted into signs of +
but other characters will not be encoded.
Assuming the browser is a little older and not supported FormData
then you’ll have to use application/x-www-form-urlencoded
and codify the past in .send(...);
, thus:
var oReq = new XMLHttpRequest();
//Defina como true
oReq.open("POST", "/url", true);
oReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//Função assíncrona que aguarda a resposta
oReq.onreadystatechange = function()
{
if (oReq.readyState === 4) {
alert(oReq.responseText);
}
};
oReq.send('nome=' + escape('João de Nobrega'));
Or:
oReq.send('nome=' + encodeURIComponent('João de Nobrega'));
If send without the application/x-www-form-urlencoded
they will go with the format RAW
and if you don’t use encodeURIComponent
or escape
it is likely that he does not recognize what comes after João
Some details here: Upload does not work $_FILES Undefined index error
Completion
Do not use synchronous because it is in disuse and browsers are issuing warnings and in the future will remove synchronous so do not use the false
, use true
or omit the third parameter (by default .open
uses true
) thus:
request.open('POST', urlBase + '/avaliar-concessionaria/lat-lng');
How the code should look
As I said synchronous will soon no longer work in modern browsers, for this you need to understand the difference of callback and Return, read this:
Your code should look like this:
function initMap() {
var idDealer = document.getElementById('id-concessionaria').value;
urlBase = document.getElementsByTagName('body')[0].getAttribute('data-base');
var myLatLng, marker, myLatLng; //Torna as variáveis acessíveis no escopo de `initMap`
var exec = function(lat, lng) {
myLatLng = { "lat": lat, "lng": lng }
map = new google.maps.Map(document.getElementById('mapa'), {
center: myLatLng,
zoom: 10
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Aqui!'
});
//Resto da função
};
var request = new XMLHttpRequest();
request.open('POST', urlBase + '/avaliar-concessionaria/lat-lng', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.onreadystatechange = function() {
if (oReq.readyState === 4 && request.status >= 200 && request.status < 400) {
var data = JSON.parse(request.responseText);
var lat = data[0];
var lng = data[1];
exec(lat, lng);
}
};
request.send("id=" + idDealer);
}
This way it will work asynchronously.
8am, the guy arrives already putting question on Stack. Not arrived right... called the PC sent the question.
– Diego Souza
I edited the reply @Zoom I hope you understand that my other comment was trying to guide you, I never give an orientation just for mimimi or because I cry a lot, it was with the sole intention of helping you ;) Please always assume good intention, I hope you can understand the asynchronous example, because as I said
sync
will be "discontinued".– Guilherme Nascimento