发送用于使用AJAX和Laravel 5.1创建的表单 [英] Send Form for create with AJAX and Laravel 5.1

查看:58
本文介绍了发送用于使用AJAX和Laravel 5.1创建的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.1开发应用程序,并且在发送Ajax请求时遇到问题,我有下一个代码:

I'm developing an application with Laravel 5.1 and I have a problem when sending ajax petition I have the next code:

查看创建:

{!!Form::open()!!}

    <div class="form-group">
   {!!Form::label('genero','Genre:')!!}
   {!!Form::text('genre',null,['id'=> 'genre','class'=>'form-control'])!!}
    </div>

    {!!link_to('#', $title = 'Create', $attributes = ['id'=> 'create','class'=>'btn btn-primary'], $secure = null)!!}
{!!Form::close()!!}

Ajax请求:

$("#create").click(function(){
 var genre = $("#genre").val();
 var route = "http://localhost:8000/genre";
 $.ajax({
   url: route,
   type: 'POST',
   dataType: 'json'
   data: {genre : genre}
  });
})

在我的路线上:

Route::resource('genre','GenreController');

但是在发送请愿书时,我遇到下一个错误:

But when send the petition I have the next error:

POST http://localhost:8000/genre 500 (Internal Server Error)

谢谢.

推荐答案

我的解决方案是下一个代码:

My solution was the next code:

$("#registro").click(function(){
var data = $("#genre").val();
var route = "http://localhost:8000/genre";
var token = document.getElementById('token').value

$.ajax({
  url: route,
  headers: {'X-CSRF-TOKEN': token},
  type: 'POST',
  dataType: 'json',
  data: {data : data}
 });
});

这篇关于发送用于使用AJAX和Laravel 5.1创建的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆