te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to use ajax in laravel 5.3 - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use ajax in laravel 5.3 - Stack Overflow

programmeradmin2浏览0评论

I am new to Laravel and am using Laravel 5.3. I want to make a text field where it will automatically suggest some data and when I select a data it will add it to an array. I want to send that array to a controller for further use. For this the

view file is as follows:

<head>
    <script src=".10.2.js"></script>
    <script src=".11.4/jquery-ui.js"></script>
    <script>
        $(document).ready(function() {
            var members = {!!  json_encode($member)  !!};
            console.log(members);
            var arr = [];
            $("#tags").autoplete({
                source: members,
                select: function (event, ui) {
                    arr.push(ui);
                    console.log(arr);
                }
            });
            $("#submit").click(function(event){
                $.ajax({
                    type: "POST",
                    url: '/storeresearch',
                    data: {selectedMembers: arr},
                    success: function( msg ) {
                        console.log(msg);
                    }
                });
            });
        });
        </script>
</head>
<body>
<form id="hu" action="/storeresearch" method="POST">
    {!! csrf_field() !!}
    <label>Research Author</label>
    <input type="text" id="tags" name="researchsupervisor_1" value="">
    <input type="submit" name="submit" id="submit" class="btn btn-primary" value="Add">
</form>
</body>

My Controller file is as follows:

public function store(Request $request){
        if($request->ajax())
        {
            $mem = $request->all();
            return response()->json($mem,200) ;
        }
        else{
            return "not found";
        } 

And web.php is as followings:

Route::post('/storeresearch','ResearchController@store');

But it seems that there is no ajax call happening. In the controller it always enters the else section. What is the problem can anyone help?

I am new to Laravel and am using Laravel 5.3. I want to make a text field where it will automatically suggest some data and when I select a data it will add it to an array. I want to send that array to a controller for further use. For this the

view file is as follows:

<head>
    <script src="http://code.jquery./jquery-1.10.2.js"></script>
    <script src="http://code.jquery./ui/1.11.4/jquery-ui.js"></script>
    <script>
        $(document).ready(function() {
            var members = {!!  json_encode($member)  !!};
            console.log(members);
            var arr = [];
            $("#tags").autoplete({
                source: members,
                select: function (event, ui) {
                    arr.push(ui);
                    console.log(arr);
                }
            });
            $("#submit").click(function(event){
                $.ajax({
                    type: "POST",
                    url: '/storeresearch',
                    data: {selectedMembers: arr},
                    success: function( msg ) {
                        console.log(msg);
                    }
                });
            });
        });
        </script>
</head>
<body>
<form id="hu" action="/storeresearch" method="POST">
    {!! csrf_field() !!}
    <label>Research Author</label>
    <input type="text" id="tags" name="researchsupervisor_1" value="">
    <input type="submit" name="submit" id="submit" class="btn btn-primary" value="Add">
</form>
</body>

My Controller file is as follows:

public function store(Request $request){
        if($request->ajax())
        {
            $mem = $request->all();
            return response()->json($mem,200) ;
        }
        else{
            return "not found";
        } 

And web.php is as followings:

Route::post('/storeresearch','ResearchController@store');

But it seems that there is no ajax call happening. In the controller it always enters the else section. What is the problem can anyone help?

Share Improve this question edited Jan 17, 2019 at 21:22 natral 1,0981 gold badge19 silver badges44 bronze badges asked Feb 10, 2017 at 8:03 Mutasim FuadMutasim Fuad 6063 gold badges12 silver badges32 bronze badges 7
  • change <input type="submit"> to <input type="button"> then try – B. Desai Commented Feb 10, 2017 at 9:02
  • Yes it fires the ajax call but the action doesn't occur. It remains in the same page – Mutasim Fuad Commented Feb 10, 2017 at 9:19
  • That is what ajax stands for. Your page will not refresh when you are dealing with ajax – B. Desai Commented Feb 10, 2017 at 9:20
  • Ow Thanks for making the conception clear. But i want both when i hit the button the ajax call and go to the controller that return something. Can you please help me about that how to do? – Mutasim Fuad Commented Feb 10, 2017 at 9:28
  • exactly what you have to do? clear it. – B. Desai Commented Feb 10, 2017 at 9:33
 |  Show 2 more ments

4 Answers 4

Reset to default 3

Your code mostly looks good. But you are missing to send a csrf token with AJAX call as you are using POST request.

You can send csrf token with AJAX call in this way:

<meta name="csrf-token" content="{{ csrf_token() }}">

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

More info: https://laravel./docs/5.3/csrf#csrf-x-csrf-token

When you hit the button, does it really fires an AJAX call? Please check that on network tab of browser.

I solved this problem by doing following

$.ajax({
                type:'POST',
                url:'your url',
                data:{_token: "{{ csrf_token() }}"
                },
                success: function( msg ) {
                }
            });

Try some thing like this:

$.ajax({
    url     : '/login',
    method  : 'post',
    data    : {
        login_username  : userName,
        password        : password
    },
    headers:
    {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    success : function(response){

    }
});

Route:

Route::post('/login',[
    'uses' => 'AdminServiceController@login'
]);

Controller method:

 public function login()
{
    $userName   = INPUT::get('login_username');
    $password   = INPUT::get('password');

    // your logic
}

What's your namespace declaration for Request ?

If it is use Illuminate\Http\Request; try use Request;

发布评论

评论列表(0)

  1. 暂无评论