dsbifvcxr458755493 2016-01-14 20:31
浏览 31

没有jquery的FUELPHP Ajax请求

I have looked everywhere, I found, that FUELPHP not handle Ajax requests, native and easily, as does RubyOnRails for example.

There must be done manually through jquery, unless I'm missing something I see is this: you have to use preventDefault () for the submit event of the form to create a post, product or whatever, and use the $ function post () to send the relevant parameters, which I think is ridiculous for a framework that claims to be based on the best ideas from other frameworks.

Please tell me if I'm wrong, I like FUELPHP, and I'm thinking about choosing it as PHP framework, but I want to be clear about this.

  • 写回答

1条回答 默认 最新

  • dtn36013 2016-02-01 05:38
    关注

    why not you can handle ajax in fuelphp like this.

    .01. create ajax request in your view or public/assets/ create javascript file,

        <script>     $('.login').on('click',function(){
                          $.ajax({
                        xhr: function () {
                        var xhr = new window.XMLHttpRequest();
                                xhr.upload.addEventListener("progress", function (e) {
                                alert("loading");
                                }, false);
                                return xhr;
                        },
                                url: "<?php echo \Uri::create('auth/login'); ?>",
                                type: "POST",
                                dataType: 'json'
                                data: {'username':$('#username').val(), 'password':$('#password').val()},
                                success: function (data) {
                                alert(data.status);
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert("error");
                                }
                        });
    
    });
    
            </script>
    

    .02. after that you can handle post data in classes/controller/ create auth.php and login method like this,

    <?php
    
    class Controller_Auth extends \Controller_Template {
    
        function action_login() {
            if (\Input::is_ajax()) {
                if (\Input::param('username') and \Input::param('password')) {
                    $username = \Input::param('username');
                    $password = md5(\Input::param('password'));
                    //check password and username or anything
                    $msg = 'fail';
                    return json_encode(array('status' => $msg));
                }
            }
            ...
        }
    
    }
    ?>
    

    you can handle data like this. i think you got something. and this is helpful.

    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)