doukanwa6872 2012-02-05 21:42
浏览 96
已采纳

在Native iPhone Phonegap应用程序上使用jQuery .ajax()或.post()提交表单?

I was wondering how will I use jQuery's .ajax() or .post() to send data to a php file on my webserver from a Phonegap Native iPhone App?
Does it have to be xml or json? Or can I just send regular html post data to the file?

If you can show me an example that would be great!

UPDATE: Thanks Drew for the stellar solution! I also found an article that explained the process pretty well too.

UPDATE 2: There is a problem with my script.

Here is my javascript. I am trying to have jQuery send my sign in form data over to my PHP file. I then tell it to grab the data from the php file and display it. But all that is coming back is "null". If I request another value such as bio, it can display it. But it cant display data sent from my form.

<script type="text/javascript">
    $(document).ready(function() {
        $("img").click(function() {
            var data = $('form#signin').serialize();
            $.ajax({
                url:'signin.php',
                type:'POST',
                data: data,
                success:function(data) {
                    $("p.test").html(data);
                    $.getJSON("signin.php", function(data) {
                        localStorage.email = data[0];

                    });
                },
                error:function(data) {
                }
            });
        });
    });
</script>

Here is my PHP

<?php

$email = $_POST["email"];

$profile = array($email, "Karl", "Clement", "Gangsta Love!", "bio bio bio bio bio bio    bio     bio bio bio bio bio bio bio bio bio bio bio", "Ottawa",     "http://a3.twimg.com/profile_images/1459354642/IMG_1560_normal.jpg");

header('Content-Type:text/json');
echo json_encode($profile);

?>

Thank you so much for your help!

  • 写回答

1条回答 默认 最新

  • douturan1807 2012-02-06 00:45
    关注

    your best bet is JSON, but you can submit like a normal form. Try some experiments -

    $.ajax({
       url:'formPage.php',
       type:'POST',
       data:'name=john&email=john@smith.com',
       success:function(data){
         console.log(data);
       },
       error:function(w,t,f){
         console.log(w+' '+t+' '+f);
       }
    });
    

    should work just fine. You could also just do JSON

        $.ajax({
           url:'formPage.php',
           type:'POST',
           data:{name:'john',email:'john@smith.com'},
           success:function(data){
             console.log(data);
           },
           error:function(w,t,f){
             console.log(w+' '+t+' '+f);
           }
        });
    

    To start I would just have your app send a request to your server and have it return a statement, that way you can see that it connects and responds, then start passing variables!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛