将 php 与 phonegap 连接起来



我正在使用phonegap制作一个应用程序,它允许.html形式,但我们需要.php格式文件,因此我们将使用PHP服务器。
在 phonegap 上制作 index.html 页面后,我没有得到任何将 phonegap 与 php 连接的示例或提示。

<html>
<head>
    <style type="text/css"></style>
    <script type="text/javascript">
    function post(){
                var first = document.getElementById("first").value; 
                var second = document.getElementById("second").value;
                var formdata = new FormData();
                formdata.append("first", first);
                formdata.append("second", second);

    var ajax = new XMLHttpRequest();
    document.getElementById('ans').innerHTML = 'Loading...';
    ajax.addEventListener("load", completeHandlerrrr, false);
    ajax.open("POST", "http://localhost/phonegap/app/post.php");
    ajax.send(formdata);
}
function completeHandlerrrr(event){
var hh= event.target.responseText; 
    document.getElementById('ans').innerHTML = hh; 
}

</script>
</head>
<body>  
 <input type="text" id="first" name="first" />
 <input type="text" id="second" name="second" /> 
 <input type="submit" id="submit" name="submit" onclick="post();"/>
<div id="one_answers"></div>
</body>
</html>

后.php

<?php
echo $_POST['first'];
echo $_POST['second'];
?>

Phonegap是一个移动应用程序开发框架,它使用HTML,CSS,JavaScript和Cordova作为本机功能。另一方面,PHP 是一种服务器端脚本语言,需要服务器上的基础设施。您不能直接使用 PHP,但可以通过在 JavaScript 文件中使用 Ajax 调用来使用来自远程服务器的 PHP 页面的结果。

在phonegap中,你可以从你的服务器使用php。

喜欢

ajax.open("POST", "http://192.168.1.1/phonegap/app/post.php"); // or you domain server www.example.com/api/post.php

而是localhost.

您可以在服务器 PHP 文件中使用正常的 MSQL 连接。

我想你确实忘记了较新的浏览器/Cordova解析器阻止了对其他网站的请求。
我的解决方案是上传包含 AJAX 槽服务器的 JS 文件并使用:
<script src="http://localhost/js/index.js"></script>

最新更新