Cordova Http插件:postJSON不是一个功能

时间:2016-04-20 19:44:02

标签: javascript cordova

我正在尝试使用cordova-HTTP插件。每当我尝试调用postJSON()方法时,我都会收到错误

LOG: [ERR] TypeError: cordovaHTTP.postJSON is not a function. (In 'cordovaHTTP.postJSON', 'cordovaHTTP.postJSON' is undefined)

我安装插件时遵循的步骤:

  1. 创建了cordova项目
  2. cordova platform add ios
  3. cordova plugin add https://github.com/brendonparker/cordova-HTTP.git
  4. 现在因为我没有apple-pc,所以我使用Evothings Workbench通过在工作台中拖放index.html文件来构建应用程序。然后我点击RUN运行应用程序。这会在'工具中产生错误。工作台的窗户。
  5. 此外,对postJSON方法的调用是在" deviceReady'之后进行的。

    谢谢!

1 个答案:

答案 0 :(得分:0)

看起来that method被称为postJson,而不是postJSON

以下是如何使用它的示例(copy-pasted from here):

var dto = {
    description: 'sending a plain old javascript object',
    child: {
        foo: 'bar',
    }
};

cordovaHTTP.postJson('https://some-server/api/', dto, { }, function(response) {
        // prints 200
        console.log(response.status);
        try {
            // response.data has already been parsed
            //response.data = JSON.parse(response.data);

            // prints test
            console.log(response.data.message);
        } catch(e) {
            console.error('JSON parsing error');
        }
    }, function(response) {
        // prints 403
        console.log(response.status);

        //prints Permission denied 
        console.log(response.error);
    });

更新: 如果使用Evothings,则必须将插件添加到' evothings'源代码本身,然后重建应用程序。