我正在Laravel网站上显示所有服务的价格表。现在,我想根据服务下拉列表中的选择更改价格表。这就是为什么我使用AJAX根据服务下拉列表的更改来调用控制器并显示所选服务的价格表的原因。
但是我在 AJAX
中遇到错误jquery-3.2.1.min.js:4 GET http://my_url/route_name?id=clinic_id 404(未找到)
HTML
<form id="check_pricelist_form" method="get">
<select class="form-control search_pricelist_Field col-md-4" id="clinic_id_pricelist" name="clinic_id">
<option value="0">All</option>
@if(count($_SESSION['clinic']) > 0)
@foreach($_SESSION['clinic'] as $data)
<option value="{{ $data->id }}">{{ $data->name }}</option>
@endforeach
@endif
</select>
</form>
脚本
$( "#clinic_id_pricelist" ).change(function() {
var clinic_id = $('#clinic_id_pricelist').val();
$.get('clinic-price-list', { id: "clinic_id"}, //getting error in this line
function(returnedData){
console.log(returnedData);
});
});
路线
Route::get('/clinic-price-list/{name}','PriceListController@detail');
如何解决?预先感谢
答案 0 :(得分:2)
尝试
$.get('clinic-price-list/clinic_id', //getting error in this line
function(returnedData){
console.log(returnedData);
});
作为请求参数传递
如果您传递的临床编号是get参数,则您的网址应类似于http://url/clinic-price-list?id=clinic_id
。
控制器
public function detail(Request $request){
$id = $request->input('id');
路线
Route::get('/clinic-price-list','PriceListController@detail');
JS
$.get('clinic-price-list', { id: "clinic_id"},
作为URL参数传递
如果您将诊所ID作为url参数传递,则您的url应该像http://url/clinic-price-list/clinic_id
控制器
public function detail(Request $request,$id){
//$id is the clinic_id
路线
Route::get('/clinic-price-list/{name}','PriceListController@detail');
JS
$.get('clinic-price-list/clinic_id',
答案 1 :(得分:1)
<img src={require(“./path/fileName.svg”} alt="some file" />
您需要在路线中输入名称。使用$.get(`clinic-price-list/${clinic_id}`,
function(returnedData){
console.log(returnedData);
});
而不是``
或""
答案 2 :(得分:0)
确保您的路线正常。 您可以直接导航到http://my_url/route_name?id=clinic_id吗?
如果可以,请确保您的请求URL正确。
$(“#clinic_id_pricelist”)。change(function(){
class UserListView(generics.ListAPIView):
lookup_vield = 'id'
serializer_class = UserDetailSerializer
def get_queryset(self):
return UserDetails.objects.all()
def list(self, request, *args, **kwargs):
res = super(UserListView, self).list(request, *args, **kwargs)
res.data = {"STATUS": "SUCCESS", "DATA": res.data}
return res
});