Drupal JSON post de PhoneGap
Pregunta
Estoy intentando enviar una solicitud POST al módulo y JSON_Server Servicios módulo de Drupal, sin embargo estoy recibiendo
{ "Error": true, "#DATA": "método no válido"}
Desde PhoneGap ejecuta archivos html de forma local en el teléfono, debe i necesidad de preocuparse por JSONP. El problema que tengo con esto es que debo datos POST, y JSONP sólo permite GET. Cualquier idea sería útil. Gracias!
//SEND REQUEST AND CALLBACK FUNCTION var req; DrupalService.prototype.request = function(dataObject, callback){ req = false; var url = DRUPAL_JSON_URL; var params = "data="+dataObject; try { req = new XMLHttpRequest(); } catch(e) { req = false; } if(req) { req.onreadystatechange = function() {//Call a function when the state changes. if(req.readyState == 4 && req.status == 200) { console.log(">> "+req.responseText); } } req.open("POST", url, false); req.send(params); } }
Solución
Así que lo he descubierto, tenía que ver con el conflicto tipos de contenido
Asegúrese de que establece como
Content-Type = application / x-www-form-urlencoded;
var DRUPAL_JSON_URL = "http://myDrupalsSite.com/services/json";
var req;
DrupalService.prototype.request = function(dataObject, callback){
var url = DRUPAL_JSON_URL;
req = false;
var params = "method=system.connect";
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
if(req) {
req.onreadystatechange = function() {//Call a function when the state changes.
if(req.readyState == 4 && req.status == 200) {
alert("test " + req.responseText)
console.log("RESPONSE "+req.responseText);
}
}
req.open("POST", url, true);
req.setRequestHeader("Content-length", params.length);
req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
req.send(params);
}
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow