Quiero que cuando se haga clic en el botón, se cree una extensión de datos, pero mi código estaba en SSJS y no puedo usar esto dentro del evento onclick, así que descubrí que AJAX podría ayudarme, pero aún no funciona
lo que he estado intentando ->
<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>en primer lugar, formatee su código correctamente y verá todos sus problemas :)
su console.log (xmlHttp.responseText) está fuera de la función onreadystatechange
xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState == 4 && xmlHttp.status == 200) alerta("estado " + xmlHttp.status); alerta("estado " + xmlHttp.readyState)}
Esto es lo que tiene en su controlador.
Además, tenga en cuenta que tendrá una alerta ("estado" + xmlHttp.status) solo cuando: xmlHttp.readyState == 4 && xmlHttp.status == 200
mientras que alert("status" + xmlHttp.readyState) se activará todas las veces.
Conclusión: ¡formatea tu código!