Good afternoon, I am trying to pass data from my form to the controller using Ajax, however I cannot do it correctly, what am I doing wrong?
Javascript
$("#btn_enviar").click(function() {
title = document.getElementById("title").value;
url_clean = document.getElementById("url_clean").value;
content = document.getElementById("content").value;
CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$.ajax({
url : 'dashboard/post/{data}',
type : 'post',
dataType: 'json',
data : {'CSRF_TOKEN':CSRF_TOKEN, 'title':title, 'url_clean':url_clean, 'content':content},
success : function (data) {
alert('send');
},
error : function() {
alert('error');
}
});
});
Routes
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('dashboard.posts.posts');
});
Route::resource('dashboard/post','dashboard\PostController');
Route::post('dashboard/post/{data}', 'dashboard\PostController@store');
when I click on the button, the console shows me the following error:
This has the validation rules for the submitted data
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StorePostPost extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|min:5|max:500',
'url_clean' => 'required|min:5|max:500',
'content' => 'required|min:5'
];
}
}
This is the controller I'm trying to send the data to (PostController)
<?php
namespace App\Http\Controllers\dashboard;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\StorePostPost;
use Illuminate\Support\Facades\Route;
class PostController extends Controller
{
public function index(){
return view("dashboard.posts.create");
}
public function create(){
return view("dashboard.posts.create");
}
public function store(StorePostPost $request){
dd($request->all());
}
}
