Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
I want to create a data extension using client server onclick event, what am I doing wrong?

I want that when the button is clicked, a data extension is created, but my code was in SSJS and I can't use this inside of onclick event, so I found that AJAX could help me, but it still not working

what i've been trying ->

<script>
    function ajax() {
     var xmlHttp;
     if(window.XMLHttpRequest){
       xmlHttp = new XMLHttpRequest;
     } else {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
     
       
  xmlHttp.onreadystatechange = function() {
  if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
  alert("status " + xmlHttp.status); alert("status " + xmlHttp.readyState)
  console.log(xmlHttp.responseText)};
  xmlHttp.open('POST','https://...', true);
  xmlHttp.send();
    }
</script> <script>
     
   
function create() {
   var DE = "MyDE"
  var CustomerKey = "CK123"
    try {
        var obj = {
            "CustomerKey" :CustomerKey,
            "Name" : DE,
            "Fields" : [
                { "Name" : "Id", "FieldType" : "Number", "IsPrimaryKey" : true, "IsRequired" : true },
                { "Name" : "MyData", "FieldType" : "Text", "MaxLength" : 50 },
                { "Name" : "Active", "FieldType" : "Boolean", "DefaultValue" : true }
            ]
        };
        DataExtension.Add(obj); 
      } catch (err){
       alert("(!) Data Extension was not created. Error message: " + err + "<br>")}
    }     
  </script> 
  <button onclick="create();ajax()">
    Request 
     </button>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

first of all, format your code properly, and you will see all you problems :)

your console.log(xmlHttp.responseText) is out of onreadystatechange function

xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState == 4 && xmlHttp.status == 200) alert("status " + xmlHttp.status); alert("status " + xmlHttp.readyState)}

Here is what you have in your handler.

Also, please pay attention that you will have alert("status " + xmlHttp.status) only when: xmlHttp.readyState == 4 && xmlHttp.status == 200

while alert("status " + xmlHttp.readyState) will be triggered all the times.

Conclusion: format your code!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda