我尝试通过Ajax将我的视图中的2
值发送给控制器并在laravel中获取结果,
这是我的script
:
<script>
$(document).ready(function() {
$('body').on('change', '#statehidden', function(e){
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
var cityname = $("#cityename").val();
var statename = $("#statename").val();
$.ajax({
url: '{{ url('getcityandstate') }}/'+encodeURI(statename)'/'+encodeURI(cityname),
type: "GET",
dataType: "json",
success:function(data) {
// $('#statehidden').empty();
// $('#statehidden').append(data);
alert('worked');
}
});
});
});
</script>
我的route
:
Route::get('/getcityandstate/{statename}/{cityname}','CartController@flatshippingcostincart');
我的controller
:
public function flatshippingcostincart(Request $request, $statename,$cityname) {
$state = $request->statename;
$city = $request->cityname;
$shipping = Shipping::where('state', '=', $state)->where('city', '=', $city)->first();
return response()->json($shipping);
}
in console
SyntaxError: missing } after property list[Learn More] //in line 1200
note: { opened at line 1199, column 15 //in line 1199
debugger
line 1199 = $.ajax({
line 1200 = url: 'http://domain.../getcityandstate/'+encodeURI(statename)'/'+encodeURI(cityname),
var statename
和var cityname
附加到我的网址?$.ajax({
似乎已关闭,为什么我会收到SyntaxError: missing }
after property list
错误?答案 0 :(得分:1)
将数据项添加到您发送的ajax对象
数据:{&#34;键:&#34;值&#34;&#34;键&#34;:&#34;值&#34;}
确保您向服务器发送帖子请求..
如果你坚持使用get将数据发送到服务器,你必须重写网址,比如
var item="mini",qty=2
then url:'www.yoursite.com?item="+item+"&quantity="+qty+"