简体   繁体   English

使用cordova从android应用程序调用rest api

[英]Call rest api from android application using cordova

I am creating a android application using cordova, I have index.Html and javascript, and I am trying to call javascript function from Html, call is not hitting javascript function.我正在使用cordova创建一个android应用程序,我有index.Html和javascript,我试图从Html调用javascript函数,调用没有命中javascript函数。 Is it any difference than web application.它与 Web 应用程序有什么区别。 Do we need to include any libraries.我们是否需要包含任何库。 Second question is:- How to call rest api from javascript.第二个问题是:-如何从javascript调用rest api。

Any help or links to reference materials is appreciated.任何帮助或参考资料的链接表示赞赏。

        <div>

        <button onclick="printIframe()">Print</button>

    </div>


<script type="text/javascript">

    printIframe: function() {

    alert("in print i frame function");

      $.ajax({
        url: "http://localhost:8178/,
        type: "GET",
        dataType: "json",
        contentType: 'application/json',
        success: function() {
            alert("Success!");
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus + jqXHR.responseText);
        }
    });

To make it work above code snippet使其在代码片段上方工作

1.Give the reference to jquery library 1.给jquery库的引用

2.Use your system ip address instead of localhost since mobile doesn't contain localhost 2.使用您的系统IP地址而不是本地主机,因为手机不包含本地主机

3.Define printIframe: function() as printIframe = function() 3.定义printIframe:function()为printIframe = function()

 <div>

        <button onclick="printIframe()">Print</button>

    </div>


<script type="text/javascript">

    printIframe = function() {

    alert("in print i frame function");

      $.ajax({
        url: "http://YourIpAddress:8178/,
        type: "GET",
        dataType: "json",
        contentType: 'application/json',
        success: function() {
            alert("Success!");
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus + jqXHR.responseText);
        }
    });

Hope it will help you希望它会帮助你

Rename your method to将您的方法重命名为

function functionname () {}

Then call it like this: functionname()然后像这样调用它: functionname()

It will work you can't create method like the one you submitted in the question in JavaScript它会起作用,您无法创建像您在 JavaScript 中的问题中提交的方法

   $(document).ready(function(){
          $("#boutonAlerte").click(function(){
            var fname = $("#fname").val();
            var lname = $("#lname").val();
            var password = $("#password").val();
            var email = $("#email").val();
            var dataString = 'fname='+ fname + '&lname='+ lname + '&password='+ password + '&email='+ email;
            $.ajax({
            url: "http://localhost/RestApi/test.php",
            type: 'POST',
            data: dataString,
            cache: false,
            success: function(result){
              alert('You have successfully registered');
              window.location = "dashboard.html";
            }
            });
          });
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM