Drupal JSON POST da PhoneGap
Domanda
Sto cercando di inviare una richiesta POST al Modulo Servizi & JSON_Server di Drupal, però io sono sempre
{ "Messaggio # errore": true, "#data": "Metodo non valido"}
Dal PhoneGap gestisce i file HTML dal livello locale al telefono, devo bisogno di preoccuparsi JSONP. Il problema che ho con questo è che devo dati POST, e JSONP consente solo per GET. Tutte le idee sarebbe utile. Grazie!
//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); } }
Soluzione
Così ho capito, aveva a che fare con tipi di contenuto contrastanti
assicurarsi di impostare come
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);
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow