找不到Laravel 5.3 Ajax网址 [英] Laravel 5.3 Ajax url not found
问题描述
我正在使用以下URL http://localhost/npr/public/admin/athletes/test/143
的屏幕上工作
在此屏幕上,我实现了以下未找到的动态下拉列表Ajax调用:
I'm working on a screen with the following url http://localhost/npr/public/admin/athletes/test/143
On this screen, I've implemented the following dynamic droplist Ajax call that's not found:
$(document).ready(function() {
$('select[name="section"]').on('change', function() {
var sectionID = $(this).val();
if(sectionID) {
$.ajax({
url: './getSportPositions'+sectionID,
method: 'get',
//data: {"_token": $('#token').val()},
dataType: "json",
success:function(data) {
$('select[name="position"]').empty();
$('select[name="position"]').append('<option value="">'+ '-- Please choose one --' +'</option>');
$.each(data, function(i, position) {
$('select[name="position"]').append('<option value="'+position.name+'">'+ position.name +'</option>');
});
}
});
}else{
$('select[name="position"]').empty();
}
});
});
路线:
Route::get('getSportPositions{id}','HomeController@getSportPositions');
我也尝试过:
Route::get('/admin/athletes/test/getSportPositions{id}','HomeController@getSportPositions');
是由于呼叫URL中的运动员ID 143引起的吗?如何解决此电话? 从错误看来,它正在尝试访问此路由:
Is it due to athlete ID 143 in the calling URL? How do I fix this call? It seems from the error that it's trying to access this route:
Route::get('/admin/athletes/test/{athlete}/', [
'uses' => 'HomeController@testAnAthlete',
'as' => 'admin.test_athlete'
]);
HTML:
<div class="form-group {{ $errors->has('position') ? ' alert alert-danger' : '' }}">
<label for="position" class="col-md-3 control-label">Position in Team</label>
<div class="col-md-6">
<select class="form-control" name="position" id="position">
@if (!$errors->has('position'))
<option selected value> -- select a team first -- </option>
@endif
</select>
</div>
@if ($errors->has('position'))
<span class="help-block">
<strong>{{ $errors->first('position') }}</strong>
</span>
@endif
</div>
推荐答案
在使用Ajax时,您必须获得类似于
When you are using Ajax you have to get url like
var APP_URL = $('meta[name="_base_url"]').attr('content');
也添加此
<meta name="_base_url" content="{{ url('/') }}">
前往标签
然后您可以使用APP_URL
then after you can use APP_URL
var url = APP_URL+"/getSportPositions/"+sectionID;
这篇关于找不到Laravel 5.3 Ajax网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!