Я начал с Ajax и, к сожалению, у меня проблема с JSON .parse (). Я хочу передать переменную по клику.
Пока я не нашел ответа на мою конкретную c проблему. Может быть, кто-то здесь может помочь мне.
Сообщение об ошибке:
Uncaught SyntaxError: Unexpected token < in JSON at position 68
at JSON.parse (<anonymous>)
at Object.<anonymous> (ajax.js?ver=5.3.2:13)
at c (jquery.js:2)
at Object.fireWith [as resolveWith] (jquery.js:2)
at l (jquery.js:2)
at XMLHttpRequest.<anonymous> (jquery.js:2)
Моя JS -функция: /admin/script/ajax.js
$("#make").change(function(){
var make = $('#make').val();
console.log('Select Value hat sich geändert zu ' + make);
$.ajax({
url: wpAjax.ajaxUrl,
data: 'make=' + make,
type: 'post'
}).done(function(model){
model = JSON.parse(model); // Here comes the error.
})
})
})
Моя PHP -Функция: администратор / скрипт / фильтр. php
<?php
if(isset($_POST['make'])) {
$model = 'Model S';
echo json_encode($model);
}
?>
Функции Wordpress. php в теме
function my_enqueue() {
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/admin/script/ajax.js', array('jquery') );
wp_localize_script( 'ajax-script', 'wpAjax',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
get_template_part( 'admin/script/filter');