Jump to content

zagazat

Neophyte
  • Posts

    1
  • Joined

  • Last visited

Everything posted by zagazat

  1. zagazat

    ajax + wordpress

    Добрый день. Возникла проблема, не могу понять где лужа. Есть набор статей. Выпадающим выводится список, далее я получаю ID статьи, и из этой статьи нужно вывести метки этой статьи. Решил делать это с помощью ajax. Но выдаёт мне undefined и всё тут. Привожу код: HTML: <div class="block_oborud"> <form method="post" id="geo"> <div class="col-xs-9" style="padding: 0;"> <select class="form-control" name="model" placeholder="Выберите модель"> <?php $args = array( 'post_type' => 'post', 'category' => 3, 'posts_per_page' => 25, 'order' => 'ASC' ); $attachments = get_posts( $args ); if ($attachments) { foreach ( $attachments as $post ) { setup_postdata($post); echo "<option value='"; echo the_ID()."'>"; //передаём ID поста на сервер для отображения городов echo the_title(); //отображаем название модели в форме echo "</option>"; } } wp_reset_postdata(); ?> </select> </div> <div class="col-xs-3" style="padding: 0;"> <input type="hidden" value="citiesView" name="action"> <input type="submit" class="btn" value="Показать"> </div> </form></div>functions.php function citiesView(){ if (isset($_POST['model'])) { $post_id = $_POST['model']; $cities = array( 'status' => 1, 'city' => wp_get_post_tags($post_id) ); echo json_encode($cities); } else { $cities = array( 'status' => 0, 'city' => 'Городов не найдено' ); echo json_encode($cities); } wp_die();}add_action('wp_ajax_postview_citiesView', 'citiesView');add_action('wp_ajax_nopriv_postview_citiesView', 'citiesView');и JS $('#geo').submit(function(e) { e.preventDefault(); var ajaxurl = 'http://' + location.host + '/wp-admin/admin-ajax.php'; var msg = $(this).serialize(); $.ajax({ type: "POST", url: ajaxurl, data: msg, success: function(html) { var res = JSON.parse(html); alert(res.status); }, error: function(html) { alert('Some Trouble'); } })})событие error в ajax-запросе не срабатывает. просто выдаётся undefined. правильно понимаю, что запрос к functions.php происходит нормально, но какая-то фигня с данными которые должны выводиться в алерте?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy