Question

I am trying to send a POST request to Drupal's Services module & JSON_Server module, however I am getting

{ "#error": true, "#data": "Invalid method " }

Since PhoneGap runs html files from locally on the phone, should i need to worry about JSONP. The issue I have with that is that I must POST data, and JSONP only allows for GET. Any ideas would be helpful. Thanks!

//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);
    }

}
Was it helpful?

Solution

So i figured it out, It had to do with conflicting content types

make sure you set it as
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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top