如何在cordova应用程序中使用php和MongoDB发布数据


How to post data in cordova app using php and MongoDB

我是新的cordova应用程序。我想问你如何从cordova应用程序使用php和MongoDB的表单发布数据。我有index.html在cordova应用程序和评论.php在c:/xampp/htdocs。我想从index.html中显示comment.php中的数据。下面是代码。即index。html和comment。php

index . html

<!DOCTYPE html> 
<html>
    <head>
        <title>jQM Complex Demo</title> 
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/> 
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/> 
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" /> 
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script> 
        <script>
            $.ajax({ 
                type : "POST", 
                url : "comment.php", 
                crossDomain: true,
                beforeSend : function() {$.mobile.loading('show')}, 
                complete : function() {$.mobile.loading('hide')}, 
                data : {email : 'email', comment : 'comment'}, 
                dataType : 'json', 
                success : function(response) { 
                    //console.error(JSON.stringify(response)); 
                    alert('Works!');
                }, 
                error : function() { 
                    //console.error("error"); 
                    alert('Not working!'); 
                } 
            }); 
        </script>
    </head> 
    <body>
        <script>
            document.addEventListener( "deviceready", function () {
                new kendo.mobile.Application( document.body, {
                    statusBarStyle: "black-translucent"
                });
            }, false );
        </script>
        <div data-role="page" id="index"> 
            <div data-theme="b" data-role="header"> 
                <h1>Index page</h1> 
            </div> 
            <div data-role="content"> 
            </div> 
        </div> 
    </body> 
</html>

comment.php

  $email = isset($_POST['email']) ? $_POST['email'] : '';
    echo $email; 
    $comment = isset($_POST['comment']) ? $_POST['comment'] : '';
    echo $comment; 

Cordova不支持PHP文件,只支持HTMLJavascript文件。所以,如果你想把mongodb作为你的默认数据库,你需要使用一个REST Service(在javascript中),它指向一个在线api(可能是php),它处理你的mongodb的数据请求(安装在在线服务器上),并提供信息给你的应用程序。

如果你喜欢一个数据库集成到你的应用程序,那么你可以使用SQLlite数据库,看看这里。

https://github.com/brodysoft/Cordova-SQLitePlugin

在命令行检查IP

ipconfig

Ethernet adapter Local Area Connection:
   Connection-specific DNS Suffix  . : 
   Link-local IPv6 Address . . . . . : fe80::19a7:4cc9:c1e8:f9ef%11
   IPv4 Address. . . . . . . . . . . : 192..168.one.three 
   Subnet Mask . . . . . . . . . . . : 255 .255 .254 .0
   Default Gateway . . . . . . . . . : 192 .168 .1 .1
现在你的服务器ip地址将是192.168.1.3

你可以访问你的c:/xampp/htdocs/comments.php使用http://192.168.one.three/comments.php在phonegap应用程序。但是如果电脑和手机在同一个网络中。

Phonegap本身在android应用程序(apk)中创建了一个服务器(localhost)。

注意:192 . . 168.。三=== 192.168.1.3

  <script>
            $.ajax({ 
                type : "POST", 
                url : "http://192.168.1.3/comment.php", 
                crossDomain: true,
                beforeSend : function() {$.mobile.loading('show')}, 
                complete : function() {$.mobile.loading('hide')}, 
                data : {email : 'email', comment : 'comment'}, 
                dataType : 'json', 
                success : function(response) { 
                    //console.error(JSON.stringify(response)); 
                    alert('Works!');
                }, 
                error : function() { 
                    //console.error("error"); 
                    alert('Not working!'); 
                } 
            }); 
        </script>