Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Estou tentando enviar um post para um web service a partir do momento que o usuário faz uma alteração no ultimo campo do formulário. Porém quando insiro a chamada da função dentro da função que "ouve" o change do campo ela não funciona.
Quando coloco a chamada da função que faz o post fora da função que ouve o change, o post é feito normalmente para o meu webservice.
Disparei um alert dentro da função do change e ele funciona normalmente. Mas o post não é feito.
Alguem pode me ajudar?
function createCORSRequest(method, url, data) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
xhr.send(data);
} else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
xhr.send(data);
} else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
//essa função está imprimindo
$(document).ready(function() {
$( "#phone" ).change(function() {
//alert( "Handler for .change() called." );
var teste = document.getElementById("firstname").value; //PARA IMPRIMIR ALERT TESTE
alert(teste); //ALERT PARA VERIFICAR SE ENTRA NA FUNCTION
var firstname = document.getElementById("firstname").value;
var email = document.getElementById("email").value;
var phone = document.getElementById("phone").value;
var url = "https://api.hubapi.com/contacts/v1/contact/?hapikey=cfcccd66-ac0e-424c-b270-a01b14ce7508";
var jsonToSend = {
"properties": [
{
"property": "email",
"value": email
},
{
"property": "firstname",
"value": firstname
},
{
"property": "phone",
"value": phone
}]
};
var xhr = createCORSRequest('POST', url, JSON.stringify(jsonToSend));
if (!xhr) {
throw new Error('CORS not supported');
}
});
});Carregando comentários...