N
N
Nikita2016-10-19 09:34:02
PHP
Nikita, 2016-10-19 09:34:02

Ajax request always throws an error, maybe I configured something wrong?

I want to add a dynamic list to my site. I took a couple of examples from the web. But each time the request is attempted, it returns an error(line 20). Took different examples but always the same. Maybe you didn't make a setting in apache? Here is the code:
JS

(function () {
  "use strict";
  jQuery(function () {
    $( '#type' ).change(function () {
      $( '#kind, #category' ).find( 'option:not(:first)' )
        .prop( 'disabled',true );
      var type_id = $( this ).val();
      if (type_id == 0) { return; }
      $.ajax({
        type: "POST",
        url: "query.php",
        dataType: "json",
        data: "query=getKinds&type_id=" + type_id,
                success: function ( data ) {
                    for ( var i = 0; i < data.length; i++ ) {
                        $( '#kind' ).append( '<option value="' + data[i].kind_id + '">' + data[i].kind + '</option>' );
                    }
                    $( '#kind' ).prop( 'disabled', false );
                },
        error: function () {
                   alert( "При выполнении запроса произошла ошибка :(" );
        }
      });
    });

    $( '#kind' ).change(function () {
      $( '#category' ).find( 'option:not(:first)' )
        .remove()
        .end()
        .prop( 'disabled',true );
      var kind_id = $( this ).val();
      var type_id = $( '#type' ).val();
      if (type_id === 0) { return; }
      $.ajax({
        type: "POST",
        url: "query.php",
        dataType: "json",
        data: "query=getCategories&type_id=" + type_id + "&kind_id=" + kind_id,
        error: function () {
          alert( "При выполнении запроса произошла ошибка :(" );
        },
        success: function ( data ) {
          for ( var i = 0; i < data.length; i++ ) {
            $( '#category' ).append( '<option value="' + data[i].category_id + '">' + data[i].category + '</option>' );
          }
          $( '#category' ).prop( 'disabled', false );
        }
      });
    });
  });
})();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton fon Faust, 2016-10-19
@Kelebvra

apparently, the error is on the query.php side,
look at the server responses in the chrome console, most likely there will be a return code other than 200.

N
Nikita, 2016-10-19
@Kelebvra

I figured everything out, I have an old version of php and there was no json_encode function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question