简体   繁体   English

使用AJAX在Cordova phonegap中加载PHP页面

[英]Load PHP page in Cordova phonegap with using AJAX

I want to show my php page in android mobile app. 我想在Android移动应用程序中显示我的php页面。 I use cordova and i added a php page in my project with this directory "php/load.php". 我使用cordova,并在我的项目中使用此目录“ php / load.php”添加了一个php页面。 I tried to show this page on index.html i tried like this but it doesn't work. 我试图在index.html上显示此页面,但我尝试这样做,但不起作用。

<button type="submit" onclick="show();">Show it</button>

<script type="text/javascript">
$(document).ready(function() {
    function show(){
            $.ajax({
                method: 'GET',
                url: 'php/load.php',
                dataType: 'text'
            })
            .done(function(data) {
                    alert(data);
                })
        alert("Pause");
    }
    });
});
</script>

And this is my php file: 这是我的php文件:

<?php
$a = "AAAAAAAA";
echo $a ;
?>

You've got syntax errors in your JavaScript that's likely causing most of the problem. 您的JavaScript中存在语法错误,这很可能会导致大多数问题。 Have a look at this and give it a try. 看看这个,试试看。

<script>
    $(function() {
        $("#btn_show").on("click", function() {
            $.ajax({
                method: 'GET',
                url: 'php/load.php',
                dataType: 'text'
            }).done(function(data) {
                alert(data);
            });
        });

    });
    </script>
    <button type="submit" id="btn_show" >Show it</button>

Four important points: 四个要点:

  1. Listen to the cordova deviceready event 收听Cordova设备就绪事件

  2. Your url in your ajax request is not local and it can't be local because on your device is no server running. 您的ajax请求中的网址不是本地的,也不能是本地的,因为您的设备上没有服务器在运行。 So it has to be something like: https://www.example.com/load.php 所以它必须是这样的: https : //www.example.com/load.php

  3. You have to whitelisten all your external sources. 您必须将所有外部资源列入白名单。

  4. You should use https. 您应该使用https。

I would suggest using json_encode function in php. 我建议在php中使用json_encode函数。 For example: 例如:

<?php
$a = "AAAAAAAA";
echo json_encode($a) ;
?>

this would return a json array object which you can use to be rendered by javascript then put on to those html element. 这将返回一个json数组对象,您可以使用它由javascript呈现,然后放到那些html元素上。

changing the url for ajax For example: "url:localhost/website/load.php" i think it must be done too for ajax to work properly. 更改ajax的URL例如:“ url:localhost / website / load.php”我认为也必须这样做以使ajax正常工作。

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

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