0

我正在尝试向 Drupal 的服务模块和 JSON_Server 模块发送 POST 请求,但是我得到了

{ "#error": true, "#data": "无效方法" }

由于PhoneGap 从手机本地运行html 文件,我是否需要担心JSONP。我遇到的问题是我必须发布数据,而 JSONP 只允许 GET。任何想法都会有所帮助。谢谢!

//发送请求和回调函数
变量请求;
DrupalService.prototype.request = 函数(数据对象,回调){

    请求=假;

    var url = DRUPAL_JSON_URL;
    var params = "data="+dataObject;

    尝试 {
        请求 = 新 XMLHttpRequest();
    } 捕捉(e){
        请求=假;
    }

    如果(请求){
        req.onreadystatechange = function() {//状态变化时调用函数。
            if(req.readyState == 4 && req.status == 200) {
                console.log(">> "+req.responseText);
            }
        }

req.open("POST", url, false);
        req.send(参数);
    }

}
4

1 回答 1

0

所以我想通了,这与冲突的内容类型有关

确保将其设置为
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);
    }
}
于 2009-11-24T23:10:18.710 回答