I am building edit post system using jquery and ajax in laravel 5.2. When i click on save changes button in my bootstrap modal, the following error is displayed:
Error: POST http://localhost:8000/edit 500 (Internal Server Error)
send @ jquery-1.12.0.min.js:4
ajax @ jquery-1.12.0.min.js:4 (anonymous function) @ myplace.js:24
dispatch @ jquery-1.12.0.min.js:3
r.handle @ jquery-1.12.0.min.js:3
js code:
$('#modal-save').on('click' , function() {
$.ajax({
method : 'POST' ,
url : url ,
data: { body: $('#post-body').val(), postid: '' , _token: token }})
.done(function(msg) {
console.log(msg['message']);
});
});
Included in view file:
<script>
var token='{{ Session::token() }}';
var url='{{ route('edit') }}' ;
</script>
<script src=".10.1/jquery.min.js"></script>
<script src=".2.1.min.js" ></script>
<script src=".3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="src/js/myplace.js"></script>
I am building edit post system using jquery and ajax in laravel 5.2. When i click on save changes button in my bootstrap modal, the following error is displayed:
Error: POST http://localhost:8000/edit 500 (Internal Server Error)
send @ jquery-1.12.0.min.js:4
ajax @ jquery-1.12.0.min.js:4 (anonymous function) @ myplace.js:24
dispatch @ jquery-1.12.0.min.js:3
r.handle @ jquery-1.12.0.min.js:3
js code:
$('#modal-save').on('click' , function() {
$.ajax({
method : 'POST' ,
url : url ,
data: { body: $('#post-body').val(), postid: '' , _token: token }})
.done(function(msg) {
console.log(msg['message']);
});
});
Included in view file:
<script>
var token='{{ Session::token() }}';
var url='{{ route('edit') }}' ;
</script>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="https://code.jquery./jquery-migrate-1.2.1.min.js" ></script>
<script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="src/js/myplace.js"></script>
Share
Improve this question
asked Jul 22, 2016 at 10:17
YaSh ChaudharyYaSh Chaudhary
2,7259 gold badges39 silver badges76 bronze badges
4
-
Internal Server Error
is your server's way of telling you you should read it's logfiles. That's the only place where you can find useful debugging information. – Oldskool Commented Jul 22, 2016 at 10:19 - @Oldskool my log file : laravel.io/bin/E31Qy – YaSh Chaudhary Commented Jul 22, 2016 at 10:26
- Looks like your CSRF token is invalid. Also see stackoverflow./questions/32738763/… – Oldskool Commented Jul 22, 2016 at 10:29
- @Oldskool ya ty , but i have already implemented the method but same error. – YaSh Chaudhary Commented Jul 22, 2016 at 10:47
1 Answer
Reset to default 3It seems like you have problems with our csrf token. Before making your ajax calls just change your ajax setup and put the token in every header you sent via ajax. This should fix your error.
First add a meta tag
<meta name="_token" content="{{ csrf_token() }}">
Then adjust ajax setup
$(function () {
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
});
or even simpler
$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });