简体   繁体   中英

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. Is it any difference than web application. Do we need to include any libraries. Second question is:- How to call rest api from javascript.

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

2.Use your system ip address instead of localhost since mobile doesn't contain localhost

3.Define printIframe: function() as 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()

It will work you can't create method like the one you submitted in the question in 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";
            }
            });
          });
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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