weixin_33716154 2015-08-04 18:49 采纳率: 0%
浏览 29

使用Laravel的Ajax POST错误

I want to submit a form of checkboxes that represent the interests of an user. When clicking a checkbox, the value of the checked interest will be sent to the database "Followers" table and the user will begin following that interest. I couldn't find a way to have one form submit multiple rows, so i decided to make each checkbox a different form and use Ajax to send the information as the user goes through the form. However, When i attempt to make a POST using Ajax I get POST http://localhost/interest net::ERR_CONNECTION_REFUSED. or ERR 500. Can someone help me? I don't understand where i'm messing up. here is my code:

i have the meta tag

<meta name="_token" content="{{ csrf_token() }}"/>

html:

{!! Form::open(array('id'=> 'form2')) !!}
                                <div class = "form-group">
                                    <div class="col-md-6">
                                        {!! Form::label('title','Title:', ['class' => 'col-md-4 control-label']) !!}
                                        {!! Form::checkbox('interest_id', '2', false, ['class' => 'formclick']) !!}
                                    </div>
                                </div>
                        <input id = "submit" type="button" value="Click Me!"  />
                                {!! Form::close() !!}

JS:

 var base_url = 'http://localhost';

$('#submit').click(function(){
   var interest = {
       interest_id : $('.formclick').val()
   }
$.ajaxSetup({
            headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
        });
$.ajax({
    type: 'POST',
    url: base_url+'/interest',
    data: interest,
    dataType: 'JSON',
    success: function() {
       alert('new interest');
    }
});

});

InterestController:

 public function store(InterestRequest $interest)
{
    $interest = new Follower(array(
        'user_id' => $interest->get('interest_id'),
        'follower_id'  => Auth::id()
    ));

    $interest->save();
}
  • 写回答

2条回答 默认 最新

  • 衫裤跑路 2015-08-04 19:38
    关注
    1. Don't use the same variable name for an parameter and a local scope variable.
    2. Are you using nginx or apache?
    3. Have you setup your .htaccess file right?
    4. What do you see when you go to http://localhost/interest/public
    5. Enable the Laravel debug mode, there you have much more information which can be of help to you and us.
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题