如何将表单数据从本机 jquery 移动应用程序发送到远程服务器?

发布于 2024-12-29 10:32:31 字数 2882 浏览 2 评论 0原文

我正在使用以下技术: - jQuery 移动 1.0.1 - 电话间隙 1.3.0 - Xcode 4.2

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

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

我需要一些插件 PhoneGap 吗? 我需要一些设置 Xcode 吗?

以下示例代码在 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);

        }
?>

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

I am using the following technologies:
- jQuery Mobile 1.0.1
- Phonegap 1.3.0
- Xcode 4.2

When I try to submit form data to a remote server, I get success using the application through the browser.

When I try to submit form data to a remote server using the application natively, I can not send.

I need some plugin PhoneGap?
I need some setup Xcode?

The following sample code was run on the 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>

In my server: (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);

        }
?>

Does anyone have any idea how to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2025-01-05 10:32:31

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

希望这能为您指明正确的方向。

Both PhoneGap and jQuery Mobile examples use AJAX request not $.post.

This blog entery uses $.ajax and jQuery Mobile
http://www.giantflyingsaucer.com/blog/?p=1948

This this other one uses a XMLHttpRequest:
http://wiki.phonegap.com/w/page/42450600/PhoneGap%20Ajax%20Sample

Hopefully that gets you pointed in the right direction.

递刀给你 2025-01-05 10:32:31

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

I don't know if it's helpful but your PHP uses "and" on line 4 but I think it should use && to test if the posted variables are both called test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文