douyongdao4046 2011-06-14 10:37
浏览 56
已采纳

使用Codeigniter在jQuery Mobile上进行Ajax请求

Currently Im trying to do ajax request using jQuery Mobile. I`m trying to use example on http://www.giantflyingsaucer.com/blog/?p=2574. Yes, it works perfectly. But problems was coming when i tried to use the code inside codeigniter.

Here is my controller :

<?php
class Ajax extends CI_Controller {
    function __construct() {

    }

    function index() {
        ?>
        <!DOCTYPE html>
        <html>
            <head>
            <title>Submit a form via AJAX</title>
              <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.css" />
              <script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
              <script src="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.js"></script>
        </head>
        <body>
            <script>
                function onSuccess(data, status)
                {
                    data = $.trim(data);
                    $("#notification").text(data);
                }

                function onError(data, status)
                {
                    // handle an error
                }        

                $(document).ready(function() {
                    $("#submit").click(function(){

                        var formData = $("#callAjaxForm").serialize();

                        $.ajax({
                            type: "POST",
                            url: "callajax",
                            cache: false,
                            data: formData,
                            success: onSuccess,
                            error: onError
                        });

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

            <!-- call ajax page -->
            <div data-role="page" id="callAjaxPage">
                <div data-role="header">
                    <h1>Call Ajax</h1>
                </div>

                <div data-role="content">
                    <form id="callAjaxForm">
                        <div data-role="fieldcontain">
                            <label for="firstName">First Name</label>
                            <input type="text" name="firstName" id="firstName" value=""  />

                            <label for="lastName">Last Name</label>
                            <input type="text" name="lastName" id="lastName" value=""  />
                            <h3 id="notification"></h3>
                            <button data-theme="b" id="submit" type="submit">Submit</button>
                        </div>
                    </form>
                </div>

                <div data-role="footer">
                    <h1>GiantFlyingSaucer</h1>
                </div>
            </div>
        </body>
        </html>
        <?php
    }

    function callajax() {
        echo "ok";
    }
}

I want to display 'ok' in notification. Im trying to call function callajax instead of call from callajax.php is this possible or do I need to create callajax.php? Ive tried to create callajax.php but it was failed.

Thank you,

  • 写回答

1条回答 默认 最新

  • douba7784 2011-06-14 10:41
    关注

    You probably want the url to be:

     url: "ajax/callajax",
    

    As you are calling the callajax function within the controller ajax.

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

报告相同问题?

悬赏问题

  • ¥15 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页