【问题标题】:How to load php file on localhost in phonegap如何在phonegap中的localhost上加载php文件
【发布时间】:2012-08-13 22:02:30
【问题描述】:

你好,我是 phonegap 的新手。我正在创建一个带有登录验证的 html 文件。我在 php 文件中的验证代码。我在android模拟器上运行。那个时候这个 php 文件不能在模拟器上运行,我该怎么办?

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>
        </title>

        <link rel="stylesheet" href="jquery.mobile-1.1.1.min.css" />

        <link rel="stylesheet" href="my.css" />
        <style>
            /* App custom styles */
        </style>
        <script src="jquery.min.js"> </script>
        <script src="jquery.mobile-1.1.1.min.js"> </script>
        <script src="my.js">
        </script>
    </head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-role="content" style="padding: 15px">
                <div data-role="fieldcontain">
                <div id="message" style="display: none;"></div>
                    <fieldset data-role="controlgroup">
                        <label for="textinput1">
                            Email:
                        </label>
                        <input name="" id="textinput1" placeholder="" value="" type="text" />
                    </fieldset>
                </div>

<script type="text/javascript">
$(document).ready(function(){
    $('#btnValidate').click(function() {

        $('#message').hide(500);

        $.ajax({
            type : 'POST',
            url : 'http://localhost/JQuery/php',
            dataType : 'json',
            data: {
                email : $('#textinput1').val()
            },
            success : function(data){
                $('#message').removeClass().addClass((data.error === true) ? 'error' : 'success')
                    .text(data.msg).show(500);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {

                $('#message').removeClass().addClass('error')
                    .text('There was an error.').show(500);
            }
        });

        return false;
    });
});
</script>

<a data-role="button" data-inline="true"  id="btnValidate" data-transition="fade" href="#page1">Submit</a>
            </div>
        </div>
        <script>
            //App custom javascript
        </script>
    </body>
</html>

这是我的 html 代码。我哪里错了,请高举我。

【问题讨论】:

  • php代码应该运行在手机上还是服务器上?

标签: php html cordova android-emulator phonegap-plugins


【解决方案1】:

为您的系统分配 IP 并使用该 IP 地址 http://183.82.166.97:9696/index.php

我在httpd.conf中是这样配置的

听着 9696 DocumentRoot "D:/PHPworkspace/HWsite" DirectoryIndex index.php 允许覆盖所有 全部允许
允许覆盖所有 全部允许

【讨论】:

    【解决方案2】:

    当您在 Android 模拟器中运行 PhoneGap 应用程序时,localhost 和 127.0.0.1 指的是the Android emulator loopback interface。可以使用IP地址10.0.2.2访问开发机

    $.ajax({
        type : 'POST',
        url : 'http://10.0.2.2/JQuery/php',
        dataType : 'json',
        data: {
            email : $('#textinput1').val()
        }...
    

    【讨论】:

    • 当我运行 html 时调用错误:function(XMLHttpRequest, textStatus, errorThrown) { $('#message').removeClass().addClass('error') .text('出现错误。').show(500); } 在我的 HTML 文件中
    • 您的计算机上是否有运行阻止端口 80 的防火墙?如果是这种情况,您必须在防火墙中打开该端口。
    【解决方案3】:

    您需要使用系统的 IP 地址(例如 192.168.1.2)或设置虚拟主机来访问您的 php 文件。 Android 模拟器作为单独的虚拟机加载,因此您无法访问 localhost 获取您的 php 文件。

    【讨论】:

    • 如何设置安卓模拟器ip地址?
    猜你喜欢
    • 2016-07-21
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    相关资源
    最近更新 更多