dongwei4652 2015-10-08 19:56
浏览 51

使用Laravel,Ajax和jquery创建按钮单击计数器

The button counter on the homepage(after logging in) is supposed to increment count each time the button is pressed. Instead on pressing the button my code is redirecting to the authentication page for failed authentications. The Ajax code does not seem to be posting to the page. I dont understand why this is happening! Been stuck with this for 2 days. Can anyone help me out? I am relatively new to this.

Here is the View(html code). Its called like.blade.php

<html>

<head>
    <meta charset="utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

<body>
    <h1>Home Page</h1>
    <?php $count=1; ?>
    <div>
        <form action="sessions/{count}" method="get" />
        <input type=submit name="count" value="Counter" />
        <br/>This button has been clicked
        <span id="counter"><?php echo $count; ?></span> times.
    </div>
    <script type="text/javascript" 
         $(document).ready(function(){ 
             $( '#counter').click(function(){ 
                $.ajax({ 
                    type : 'POST', 
                    url: "sessions.{count}", 
                    data: like:like, 
                    dataType: 'text', 
                    async: true 
                    success: function update_text(data) { 
                        alert( "Counter clicked"); 
                    };
                });                         
                return false;
              }); 
          }); 
    </script>
< /body>
</html >

This is the method where I am incrementing the counter and returning it :

Route::get('sessions.like', function () {
    return View::make('sessions.like');
});

Route::get('sessions/{count}', function ($count) {
    if (isset($count)) {
        $count = $count + 1;
    } else {
        $count = 1;
    }

    return View::make('sessions.like')->with('sessions', $count);
});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 鸿业暖通修改详细负荷时闪退
    • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体