奇怪的jQuery / Laravel 5.1 REQUEST行为

时间:2016-01-08 15:53:40

标签: jquery laravel-5

我遇到了一个非常奇怪的问题。我的路线设置为POST,我的jQuery ajax请求设置为POST,浏览器说它正在发送POST,但控制器正在告诉我它&#39} ; s通过GET接收。

这是我的路线;因为我收到了methodNotAllowed个错误,我已将其更改为REQUEST进行测试 - 这样我才能看到发生了什么。

Route::request('ajax/team-comments',   'UserWalkController@addTeamComments');

我收到的路线设置为发布

的错误
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 206
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158

点击提交按钮后,来自浏览器的“网络”标签...

Browser result

在我的控制器中,我使用dd()查看发生了什么

public function addTeamComments(Request $request)
{
    dd($_SERVER['REQUEST_METHOD']);

然后吐出

"GET"

这是我的ajax ......

我正在使用jQuery 1.10.2

$('.btn-team-comments').on('click',function(e) {

    e.preventDefault();

    var id      = $(this).data('id');
    var token   = $('meta[name="csrf-token"]').attr('content');
    var comment = $('#comment-' + id).val();

    $.ajax({
        method   : 'POST',
        url      : '/ajax/team-comments',
        cache    : false,
        data     : {
                    'id'      : id,
                    'comment' : comment,
                    '_token'  : token
                   },
        contentType  : false,
        processData  : false,
        headers      : {'X-CSRF-TOKEN': token},
        dataType : 'json',
        success  : function(response) {

            if(response.type == 'success') {
                // doing stuff here...
            } else {
                alert('Oops');
            }
        }
    });
});

我应该注意,我没有使用<form>,我只是抓住textarea的内容然后发送

<div id="formcomment<?php echo $goal_id ?>">
    <div class="form-group">
        <textarea name="comment" id="comment-<?php echo $goal_id ?>" class="form-control" rows="2" placeholder="Add Your Comment..."></textarea>
    </div>
    <button type="button" class="btn btn-success btn-team-comments" id="btn-team-comments-<?php echo $goal_id ?>" data-id="<?php echo $goal_id ?>"><i class="fa fa-check"></i></button>
</div>

有关正在发生的事情以及如何解决问题的任何想法?

0 个答案:

没有答案