我一直收到此错误" 500(内部服务器错误)发送@jquery.min.js:5ajax @jquery.min.js:5(匿名)@(索引):260dispatch @jquery.min.js :3v.handle @ jquery.min.js:3"每当我尝试在laravel中使用ajax发布帖子请求时,我会真的很感激一些帮助。 以下是我视图中的代码:
<form action="" >
<div class="switch-field">
<h5>Academic Level</h5>
<input class="changedVal" type="radio" id="highschool" name="academic_level" value="Highschool" />
<label for="highschool">High School</label>
<input class="changedVal" type="radio" id="college" name="academic_level" value="College" />
<label for="college">College</label>
<input class="changedVal" type="radio" id="university" name="academic_level" value="University" />
<label for="university">University</label>
<input class="changedVal" type="radio" id="masters" name="academic_level" value="Masters" />
<label for="masters">Master's</label>
<input class="changedVal" type="radio" id="phd" name="academic_level" value="Phd" />
<label for="phd">PhD</label>
</div>
<div class="switch-field">
<h5>Spacing</h5>
<input class="changedVal" type="radio" id="double_spaced" name="spacing" value="Double Spaced" />
<label for="double_spaced">Double Spaced</label>
<input class="changedVal" type="radio" id="single_spaced" name="spacing" value="Single Spaced" />
<label for="single_spaced">Single Spaced</label>
</div>
<div>
<h5>No. of pages</h5>
<input class="changedVal" type="number" name="no_pages" min="1" max="10" value="1" />
</div>
<div class="switch-field" style="margin-bottom:25px;">
<h5>Deadline</h5>
<input class="changedVal" type="radio" id="14_days" name="deadline" value="14 Days" />
<label for="14_days">14 Days</label>
<input class="changedVal" type="radio" id="7_days" name="deadline" value="7 Days" />
<label for="7_days">7 Days</label>
<input class="changedVal" type="radio" id="5_days" name="deadline" value="5 Days" />
<label for="5_days">5 Days</label>
<input class="changedVal" type="radio" id="3_days" name="deadline" value="3 Days" />
<label for="3_days">3 Days</label>
<input class="changedVal" type="radio" id="48_hrs" name="deadline" value="48 Hrs" />
<label for="48_hrs">48 Hrs</label>
<input class="changedVal" type="radio" id="24_hrs" name="deadline" value="24 Hrs" />
<label for="24_hrs">24 Hrs</label>
</div>
<input style="margin-left:-300px;margin-top:0px;width:200px;border:none;background-color:transparent;font-size:30px;color:#32c5d2;" type="text" id="testVal">
<script type="text/javascript" >
var coun=0;
$('.changedVal').change(function(){
var level=$('input[name=academic_level]:checked').val();
var spacing=$('input[name=spacing]:checked').val();
var pages=$('input[name=no_pages]').val();
var deadline=$('input[name=deadline]:checked').val();
var data={academic_level:level,spacing:spacing,pages:pages,deadline:deadline};
console.log(data);
$.ajax({
url: '/pricecalculator',
type: 'post',
data: data,
success: function(msg){
console.log('data returned='+msg);
$('#testVal').val(msg);
}
});
});
</script>
</form>
以下代码是我的路线:
Route::post('/pricecalculator', array('uses' => 'MainController@priceCalculator'));
以下代码是我的控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use App\Http\Controllers\Controller;
use App\Prices;
use View;
class MainController extends Controller
{
public function priceCalculator(Request $request)
{
$request->all();
dd($request);
}
}
答案 0 :(得分:0)
当您在laravel中发布帖子请求时,您必须在ajax标题中包含csrf token
值,如:
$.ajax({
url: 'login',
method: 'post',
data: {
key: value
},
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(response){
}
});
如果没有这个,你将得到 500(内部服务器错误)