【问题标题】:Ajax Request on jQuery Mobile using with CodeginiterjQuery Mobile 上使用 Codeigniter 的 Ajax 请求
【发布时间】:2011-06-14 10:37:12
【问题描述】:

目前我正在尝试使用 jQuery Mobile 进行 ajax 请求。我正在尝试在http://www.giantflyingsaucer.com/blog/?p=2574 上使用示例。是的,它工作得很好。但是当我尝试使用 codeigniter 中的代码时,问题就来了。

这是我的控制器:

<?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";
    }
}

我想在通知中显示“确定”。我m trying to call function callajax instead of call from callajax.php is this possible or do I need to create callajax.php? Ive 尝试创建 callajax.php 但失败了。

谢谢,

【问题讨论】:

    标签: php jquery ajax codeigniter jquery-mobile


    【解决方案1】:

    您可能希望网址为:

     url: "ajax/callajax",
    

    当您在控制器 ajax 中调用 callajax 函数时。

    【讨论】:

    • 哇,它有效!谢谢..如果我尝试从 callajax() 调用 xml 或 json 是否一样?当然在调用 xml 视图并将其加载到 callajax() 之后。
    • 为此,您可以将 ajax/callajax 视为您的“页面”。 IE。如果您访问 yourdomain.com/ajax/callajax,您将看到消息“ok”。所以你可以在那个函数中做任何你想做的事情,它会正常返回。链接到 xml 文件,你会做正常。如果他们在 yourdomain.com/xml/my-xml.xml 然后只需链接到 site_url(xml/my-xml.xml)。
    • 谢谢你的另一个好答案:D
    • 不用担心。我是否需要让您知道索引函数中的所有内容都应该在视图中?我猜您简化了示例,所以请忽略我。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多