【问题标题】:how can I send the form data from a native jquery mobile app to a remote server?如何将表单数据从原生 jquery 移动应用程序发送到远程服务器?
【发布时间】:2012-01-30 01:09:32
【问题描述】:

我正在使用以下技术: - jQuery 移动 1.0.1 -Phonegap 1.3.0 - Xcode 4.2

当我尝试将表单数据提交到远程服务器时,我通过浏览器使用应用程序获得了成功。

当我尝试使用本机应用程序将表单数据提交到远程服务器时,我无法发送。

我需要一些插件PhoneGap? 我需要一些设置 Xcode?​​p>

以下示例代码在 iPhone Simulator 5.0 上运行:

<html> 
<head> 
    <title>Page Title</title> 
    <link rel="stylesheet" href="scripts/jquery.mobile-1.0.min.css" />
    <script type="text/javascript" src="scripts/jquery.js"></script>
    <script type="text/javascript" src="scripts/jquery.mobile-1.0.min.js"></script>

    <script>
        $(document).ready(function() {
                $('#loginForm').submit(function() {
                            $('#output').html('Connecting....');
                            var postTo = 'http://myserver/login.php';

                            $.post(postTo,{username: $('[name=username]').val() , password: $('[name=password]').val()} , 
                                function(data) {

                                if(data.message) {
                                    $('#output').html(data.message);
                                } else {
                                    $('#output').html('Could not connect');
                                }

                                },'json');

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

    </head> 

    <body> 

        <!-- Start of first page -->
        <div data-role="page" id="foo">

            <div data-role="header">
                <h1>Foo</h1>
            </div><!-- /header -->

            <div data-role="content">   

                <p id="output"></p>

                <form method="post" id="loginForm">
                Username: <input type="text" name="username"> <br /> <br />
                Password: <input type="password" name="password"> <br />

                <input type="submit" value="Login">

                </form>


            </div><!-- /content -->

            <div data-role="footer">
                <h4>Page Footer</h4>
            </div><!-- /header -->
        </div><!-- /page -->


    </body>

</html>

在我的服务器中:(login.php)

    <?php
    if(isset($_POST['username']) and isset($_POST['password'])) {

            if ($_POST['username'] == 'test' and $_POST['password'] == 'test') {

            $data['success'] = true;
            $data['message'] = 'Login succesful';

            } else {

            $data['success'] = false;
            $data['message'] = 'Login failed';

            }

        // return json
        echo json_encode($data);

        }
?>

有人知道如何解决这个问题吗?

【问题讨论】:

  • 提交表单时会发生什么? AJAX 回调是否运行?如果没有,请添加error 回调以查看问题所在。

标签: iphone ios jquery-mobile cordova xcode4.2


【解决方案1】:

PhoneGap 和 jQuery Mobile 示例都使用 AJAX 请求而不是 $.post。

本博客使用$.ajax和jQuery Mobile http://www.giantflyingsaucer.com/blog/?p=1948

这个另一个使用 XMLHttpRequest: http://wiki.phonegap.com/w/page/42450600/PhoneGap%20Ajax%20Sample

希望这能让你指出正确的方向。

【讨论】:

  • $.post 和 $.ajax({ type : 'post' }) 是一样的
  • 没错,但我很想改变它以匹配 Giantflyingsauscer 的例子,只是为了 100% 确定。
  • 感谢您对文章的指示。我测试了代码并得到了 401 错误响应我将继续尝试解决感谢您的帮助
【解决方案2】:

我不知道这是否有帮助,但您的 PHP 在第 4 行使用“and”,但我认为它应该使用 && 来测试发布的变量是否都称为 test。

【讨论】:

  • 对不起,错了。 and 和 && 都是逻辑运算符,基本含义相同,只是优先级不同。
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2017-12-28
  • 2016-12-23
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多