0

I'm trying to put 2 scripts in 1 js file and i'm getting:

SyntaxError: missing } after property list
note: { opened at line 9, column 19

But as far as I check all the curly brackets are closed, not sure what the real issue is.

Code

// country
jQuery( document ).ready( function( $ ) {
    $('select[name="country"]').on('change', function() {
      $.ajaxSetup({
          headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
      });
       var CountryId = $(this).val();
        if(CountryId) {
            $.ajax({
              url: '{{ url('getprovinces') }}/'+encodeURI(CountryId),
              type: "GET",
              dataType: "json",
              success:function(data) {
                $('select[name="province"]').empty();
                var options = data.map(function(state) {
                    return $("<option class='form-control'>").val(state.id)
                                        .text(state.name);
                });
                $('select[name="province"]').empty().append(options);
              }
            });
        }else{
          $('select[name="province"]').empty().append("<option class='form-control' value='' selected>Select</option>");
        }
    });
});

error comes from this line:

url: '{{ url('getprovinces') }}/'+encodeURI(CountryId),

Any idea?

11
  • Are you using laravel framework? Commented Dec 24, 2018 at 4:33
  • I think, your code is not in laravel blade, it is in a separate js file. Don't use blade syntax in javascript Commented Dec 24, 2018 at 4:35
  • @Shidersz yes.. Commented Dec 24, 2018 at 4:37
  • @TamilSelvanC yes it is in separate file Commented Dec 24, 2018 at 4:38
  • "{{ url('getprovinces') }}/"+encodeURI(CountryId) This will work Commented Dec 24, 2018 at 4:38

3 Answers 3

1

You need to escape the quotes or use a double-quoted string. Otherwise JS thinks the string ends on the quote after url(' then gets confused when a variable name pops up.

Sign up to request clarification or add additional context in comments.

Comments

0

Try like that:

// country
jQuery( document ).ready( function( $ ) {
    $('select[name="country"]').on('change', function() {
      $.ajaxSetup({
          headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
      });
       var CountryId = $(this).val();
        if(CountryId) {
            $.ajax({
              url: '{{' + url('getprovinces') + '}}/' + encodeURI(CountryId),
              type: "GET",
              dataType: "json",
              success:function(data) {
                $('select[name="province"]').empty();
                var options = data.map(function(state) {
                    return $("<option class='form-control'>").val(state.id)
                                        .text(state.name);
                });
                $('select[name="province"]').empty().append(options);
              }
            });
        }else{
          $('select[name="province"]').empty().append("<option class='form-control' value='' selected>Select</option>");
        }
    });
});

7 Comments

what is value of url('getprovinces')?
this is the route Route::get('/getprovinces/{countryid}','Front\AddressLists@getprovinces'); it returns provinces as json
so far, above my code is not problem. there should be problem url('getprovinces')
wth..!! why did you pass those kinda json data to url? better post method instead for those getprovinces data.
so you suggest i change my route to post?
|
0

Try

url: url('getprovinces') + '/' + encodeURI(CountryId),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.