通过Ajax url传递多个变量

时间:2018-03-25 07:45:44

标签: php ajax laravel

我尝试通过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),

问题

  1. 如何将var statenamevar cityname附加到我的网址?
  2. 我的$.ajax({似乎已关闭,为什么我会收到SyntaxError: missing } after property list错误?

1 个答案:

答案 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+"