我不斷收到此錯誤「500(內部服務器錯誤)send @ jquery.min.js:5ajax @ jquery.min.js:5(anonymous)@ (index):260dispatch @ jquery.min.js:3v.handle @ jquery.min.js:3「每當我嘗試在laravel中使用ajax發送一個post請求,並且會真正感謝一些幫助。 下面是我的看法代碼:Laravel 5 Ajax 500(內部服務器錯誤)發出post請求時
<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' => '[email protected]'));
下面的代碼是我的控制器:
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);
}
}
您需要發送的CSRF令牌在你的Ajax調用的頭。按照這個https://stackoverflow.com/questions/45668337/how-can-i-get-new-csrf-token-in-laravel-by-using-ajax/45668376#45668376 –
謝謝隊友......它的工作 – Tovo
沒關係。很高興它可以幫助 –