Drupal JSON POST from PhoneGap

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

}

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);
    }
}
链接地址: http://www.djcxy.com/p/22300.html

上一篇: XmlHttpRequest错误:Access不允许源null

下一篇: PhoneGap的Drupal JSON POST