Descripción
Este texto permite enviar los valores de varios campos, en formato json a un servidor externo a Drupal 8
Instrucciones
- Crear el tipo de contenido con los campos que quieres enviar a tu servidor externo
- Añadir el archivo js a tu theme y poner el código dentro, asegurándote de sustituir los valores como URL y campos
- Si la conexión funciona imprimirá la información en la consola
Código
/*===========================================
COPY PASTE SUGGESIONS
============================================*/
(function ($, Drupal) {
Drupal.behaviors.myModuleBehavior = {
attach: function (context, settings) {
// Post to External Server
const url = "https://CAMBIAR POR LA URL DE TU SERVIDOR EXTERNO";
const data = {};
data.field_name = "string";
data.field_date = "string";
data.field_title = "string";
data.field_body = "string";
const json = JSON.stringify(data);
const xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.onload = function () {
const sugerencias = JSON.parse(xhr.responseText);
if (xhr.readyState === 4 && xhr.status === "201") {
//console.table(sugerencias);
//document.getElementById("mitexto").innerHTML = this.sugerencias;
document.getElementById("mitexto").innerHTML = this.responseText;
} else {
//console.error(sugerencias);
document.getElementById("mitexto").innerHTML = this.responseText;
}
}
xhr.send(json);
};
})(jQuery, Drupal);