Vivek Titwal Vivek Titwal - 1 year ago 258
Ajax Question

unprocessable entity error (ajax) (laravel 5.2)

I am working on laravel 5.2. I want to edit my group name using ajax request but i am getting an error:"Unprocessable entity error". I am able to fetch the group name using jquery in my textarea but when i click on the save button, i am getting an error each time which is shown below:enter image description here

My controller:

public function groupEditGroup(Request $request){
'groupname' => 'required|min:5'
return response()->json(['new_name' => $group->name],200);

My view:

<section class="col-md-offset-1 col-md-9 special"
data-groupid="{{ $group->id }}">
<a id="hash" href="" style="font-size:20px;text-decoration:none;">
<button type="button" style="margin-top:5px;" class="btn btn-warning
dropdown-toggle" data-toggle="dropdown"><span id="removesign"
class="glyphicon glyphicon-chevron-down"></span></button>

<ul class="dropdown-menu" role="menu">
<li role="presentation"><a id="edit" style="font-weight:bold;
color:black;"role="menuitem" tabindex="-1">Edit Group Name</a></li>
<li role="presentation"><a style="font-weight:bold; color:black;"
role="menuitem" tabindex="-1" href="{{ URL('/groups/'.$group->id)
}}">Leave This Group</a></li>


<!-- Modal -->
<div id="edit-modal" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;
<h4 class="modal-title" style="font-weight:bold; color:black;">
Edit Group Name</h4>
<div class="modal-body">
<div class="form-group">
<textarea class="form-control" name="edit-group" id="edit-group">
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-lg"
data-dismiss="modal" id="modal-save">Save</button>


var token='{{ Session::token() }}';
var url='{{ route('edit') }}';

My Javascript:

var groupId= 0;
var postBodyElement=null;
menu').find('li').find('#edit').on('click', function(event){
var groupName=postBodyElement.textContent;


$(function () {
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }

data:{ body: $('#edit-group').val(), groupId: groupId ,
_token: token }



Answer Source
        url:url, //Changed the 'body' index to 'groupname' in data 
        data:{ groupname: $('#edit-group').val(), groupId: groupId , 
       _token: token }

         .fail(function(msg) { 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download