Unexpected token < in JSON error with Wordpress autocomplete Jquery












0














I am trying to get a JQuery autocomplete script to work in Wordpress. I believe I have everything set up correctly as I do get an error when I enter data into the input field, but then I get the following error. So, I am supposing something is wrong with the JSON, but I am just not sure how to debug that.



Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Function.n.parseJSON (jquery.js?ver=1.12.4:4)
at Function.a.parseJSON (jquery-migrate.min.js?ver=1.4.1:2)
at Object._transformResult [as transformResult] (jquery.autocomplete.js?ver=4.9.8:133)
at Object.<anonymous> (jquery.autocomplete.js?ver=4.9.8:584)
at i (jquery.js?ver=1.12.4:2)
at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
at y (jquery.js?ver=1.12.4:4)
at XMLHttpRequest.c (jquery.js?ver=1.12.4:4)


Here is the JSON that is being returned:



["Hello world!","Email Notification","Email Notification","Formidable Style","Email Notification","Email Notification","Email Notification","Chapter Maintenance - Admin View","Chapter Info - All","Featured Members"]


From other posts I have looked at the comment is that it isn't being parsed correctly, but I can't determine from my research how to resolve that.



Here is the Jquery:



jQuery(document).ready(function($) {    

$('#autocomplete-id').autocomplete({
source: function(name, response) {
$.ajax({
type: 'POST',
dataType: 'json',
url: '/wp-admin/admin-ajax.php',
data: 'action=get_listing_names&name='+name,

success: function(data) {
response(data);

}
});

}
});

});


Here is the function in Wordpress's functions.php I am using to return the JSON via the admin-ajax.php



add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {
global $wpdb; //get access to the WordPress database object variable

//get names of all businesses
$name = $wpdb->esc_like(stripslashes($_POST['name'])).'%'; //escape for use in LIKE statement
$sql = "select post_title
from $wpdb->posts
where post_status='publish' LIMIT 10";

$sql = $wpdb->prepare($sql, $name);

$results = $wpdb->get_results($sql);

//copy the business titles to a simple array
$titles = array();
foreach( $results as $r )
$titles = addslashes($r->post_title);



echo json_encode($titles); //encode into JSON format and output

die(); //stop "0" from being output
}


I have tried to console.log(data) into the success area, but all I get back is the error without any data.



When I look at the Network tab I see the requests from when I enter a letter into the input box, but all they show are ?query=a or ?query=b.



If I click on one of those it loads up the same page I am currently on (with the autocomplete input box) with ?query=a tacked onto the end of it, so that doesn't look right.



I'm just not sure why though, if it is set up correctly to get the data from admin-ajax.php.



My main question is, what can I do to debug this further?










share|improve this question
























  • '/wp-admin/admin-ajax.php' returns HTML and not JSON
    – Andreas
    Nov 20 at 17:52






  • 1




    In the Network tab, look at the Response tab to see what /wp-admin/admin-ajax.php?action=get_listing_names&name=name is returning.
    – aynber
    Nov 20 at 17:53










  • Not the issue, but pass you data like this data: {action: get_listing_names, name: name},
    – RiggsFolly
    Nov 20 at 17:54










  • @aynber. Hmm, actually, there isn't anything from admin-ajax.php there.
    – CRAIG
    Nov 20 at 17:56










  • @Andreas I am encoding the result as JSON. Are you saying you can't echo JSON from admin-ajax.php? I thought that was the only acceptable way to utilize ajax in Wordpress? Am I missing something here?
    – CRAIG
    Nov 20 at 17:58
















0














I am trying to get a JQuery autocomplete script to work in Wordpress. I believe I have everything set up correctly as I do get an error when I enter data into the input field, but then I get the following error. So, I am supposing something is wrong with the JSON, but I am just not sure how to debug that.



Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Function.n.parseJSON (jquery.js?ver=1.12.4:4)
at Function.a.parseJSON (jquery-migrate.min.js?ver=1.4.1:2)
at Object._transformResult [as transformResult] (jquery.autocomplete.js?ver=4.9.8:133)
at Object.<anonymous> (jquery.autocomplete.js?ver=4.9.8:584)
at i (jquery.js?ver=1.12.4:2)
at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
at y (jquery.js?ver=1.12.4:4)
at XMLHttpRequest.c (jquery.js?ver=1.12.4:4)


Here is the JSON that is being returned:



["Hello world!","Email Notification","Email Notification","Formidable Style","Email Notification","Email Notification","Email Notification","Chapter Maintenance - Admin View","Chapter Info - All","Featured Members"]


From other posts I have looked at the comment is that it isn't being parsed correctly, but I can't determine from my research how to resolve that.



Here is the Jquery:



jQuery(document).ready(function($) {    

$('#autocomplete-id').autocomplete({
source: function(name, response) {
$.ajax({
type: 'POST',
dataType: 'json',
url: '/wp-admin/admin-ajax.php',
data: 'action=get_listing_names&name='+name,

success: function(data) {
response(data);

}
});

}
});

});


Here is the function in Wordpress's functions.php I am using to return the JSON via the admin-ajax.php



add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {
global $wpdb; //get access to the WordPress database object variable

//get names of all businesses
$name = $wpdb->esc_like(stripslashes($_POST['name'])).'%'; //escape for use in LIKE statement
$sql = "select post_title
from $wpdb->posts
where post_status='publish' LIMIT 10";

$sql = $wpdb->prepare($sql, $name);

$results = $wpdb->get_results($sql);

//copy the business titles to a simple array
$titles = array();
foreach( $results as $r )
$titles = addslashes($r->post_title);



echo json_encode($titles); //encode into JSON format and output

die(); //stop "0" from being output
}


I have tried to console.log(data) into the success area, but all I get back is the error without any data.



When I look at the Network tab I see the requests from when I enter a letter into the input box, but all they show are ?query=a or ?query=b.



If I click on one of those it loads up the same page I am currently on (with the autocomplete input box) with ?query=a tacked onto the end of it, so that doesn't look right.



I'm just not sure why though, if it is set up correctly to get the data from admin-ajax.php.



My main question is, what can I do to debug this further?










share|improve this question
























  • '/wp-admin/admin-ajax.php' returns HTML and not JSON
    – Andreas
    Nov 20 at 17:52






  • 1




    In the Network tab, look at the Response tab to see what /wp-admin/admin-ajax.php?action=get_listing_names&name=name is returning.
    – aynber
    Nov 20 at 17:53










  • Not the issue, but pass you data like this data: {action: get_listing_names, name: name},
    – RiggsFolly
    Nov 20 at 17:54










  • @aynber. Hmm, actually, there isn't anything from admin-ajax.php there.
    – CRAIG
    Nov 20 at 17:56










  • @Andreas I am encoding the result as JSON. Are you saying you can't echo JSON from admin-ajax.php? I thought that was the only acceptable way to utilize ajax in Wordpress? Am I missing something here?
    – CRAIG
    Nov 20 at 17:58














0












0








0







I am trying to get a JQuery autocomplete script to work in Wordpress. I believe I have everything set up correctly as I do get an error when I enter data into the input field, but then I get the following error. So, I am supposing something is wrong with the JSON, but I am just not sure how to debug that.



Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Function.n.parseJSON (jquery.js?ver=1.12.4:4)
at Function.a.parseJSON (jquery-migrate.min.js?ver=1.4.1:2)
at Object._transformResult [as transformResult] (jquery.autocomplete.js?ver=4.9.8:133)
at Object.<anonymous> (jquery.autocomplete.js?ver=4.9.8:584)
at i (jquery.js?ver=1.12.4:2)
at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
at y (jquery.js?ver=1.12.4:4)
at XMLHttpRequest.c (jquery.js?ver=1.12.4:4)


Here is the JSON that is being returned:



["Hello world!","Email Notification","Email Notification","Formidable Style","Email Notification","Email Notification","Email Notification","Chapter Maintenance - Admin View","Chapter Info - All","Featured Members"]


From other posts I have looked at the comment is that it isn't being parsed correctly, but I can't determine from my research how to resolve that.



Here is the Jquery:



jQuery(document).ready(function($) {    

$('#autocomplete-id').autocomplete({
source: function(name, response) {
$.ajax({
type: 'POST',
dataType: 'json',
url: '/wp-admin/admin-ajax.php',
data: 'action=get_listing_names&name='+name,

success: function(data) {
response(data);

}
});

}
});

});


Here is the function in Wordpress's functions.php I am using to return the JSON via the admin-ajax.php



add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {
global $wpdb; //get access to the WordPress database object variable

//get names of all businesses
$name = $wpdb->esc_like(stripslashes($_POST['name'])).'%'; //escape for use in LIKE statement
$sql = "select post_title
from $wpdb->posts
where post_status='publish' LIMIT 10";

$sql = $wpdb->prepare($sql, $name);

$results = $wpdb->get_results($sql);

//copy the business titles to a simple array
$titles = array();
foreach( $results as $r )
$titles = addslashes($r->post_title);



echo json_encode($titles); //encode into JSON format and output

die(); //stop "0" from being output
}


I have tried to console.log(data) into the success area, but all I get back is the error without any data.



When I look at the Network tab I see the requests from when I enter a letter into the input box, but all they show are ?query=a or ?query=b.



If I click on one of those it loads up the same page I am currently on (with the autocomplete input box) with ?query=a tacked onto the end of it, so that doesn't look right.



I'm just not sure why though, if it is set up correctly to get the data from admin-ajax.php.



My main question is, what can I do to debug this further?










share|improve this question















I am trying to get a JQuery autocomplete script to work in Wordpress. I believe I have everything set up correctly as I do get an error when I enter data into the input field, but then I get the following error. So, I am supposing something is wrong with the JSON, but I am just not sure how to debug that.



Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Function.n.parseJSON (jquery.js?ver=1.12.4:4)
at Function.a.parseJSON (jquery-migrate.min.js?ver=1.4.1:2)
at Object._transformResult [as transformResult] (jquery.autocomplete.js?ver=4.9.8:133)
at Object.<anonymous> (jquery.autocomplete.js?ver=4.9.8:584)
at i (jquery.js?ver=1.12.4:2)
at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
at y (jquery.js?ver=1.12.4:4)
at XMLHttpRequest.c (jquery.js?ver=1.12.4:4)


Here is the JSON that is being returned:



["Hello world!","Email Notification","Email Notification","Formidable Style","Email Notification","Email Notification","Email Notification","Chapter Maintenance - Admin View","Chapter Info - All","Featured Members"]


From other posts I have looked at the comment is that it isn't being parsed correctly, but I can't determine from my research how to resolve that.



Here is the Jquery:



jQuery(document).ready(function($) {    

$('#autocomplete-id').autocomplete({
source: function(name, response) {
$.ajax({
type: 'POST',
dataType: 'json',
url: '/wp-admin/admin-ajax.php',
data: 'action=get_listing_names&name='+name,

success: function(data) {
response(data);

}
});

}
});

});


Here is the function in Wordpress's functions.php I am using to return the JSON via the admin-ajax.php



add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {
global $wpdb; //get access to the WordPress database object variable

//get names of all businesses
$name = $wpdb->esc_like(stripslashes($_POST['name'])).'%'; //escape for use in LIKE statement
$sql = "select post_title
from $wpdb->posts
where post_status='publish' LIMIT 10";

$sql = $wpdb->prepare($sql, $name);

$results = $wpdb->get_results($sql);

//copy the business titles to a simple array
$titles = array();
foreach( $results as $r )
$titles = addslashes($r->post_title);



echo json_encode($titles); //encode into JSON format and output

die(); //stop "0" from being output
}


I have tried to console.log(data) into the success area, but all I get back is the error without any data.



When I look at the Network tab I see the requests from when I enter a letter into the input box, but all they show are ?query=a or ?query=b.



If I click on one of those it loads up the same page I am currently on (with the autocomplete input box) with ?query=a tacked onto the end of it, so that doesn't look right.



I'm just not sure why though, if it is set up correctly to get the data from admin-ajax.php.



My main question is, what can I do to debug this further?







php jquery json ajax wordpress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 18:05

























asked Nov 20 at 17:50









CRAIG

3761515




3761515












  • '/wp-admin/admin-ajax.php' returns HTML and not JSON
    – Andreas
    Nov 20 at 17:52






  • 1




    In the Network tab, look at the Response tab to see what /wp-admin/admin-ajax.php?action=get_listing_names&name=name is returning.
    – aynber
    Nov 20 at 17:53










  • Not the issue, but pass you data like this data: {action: get_listing_names, name: name},
    – RiggsFolly
    Nov 20 at 17:54










  • @aynber. Hmm, actually, there isn't anything from admin-ajax.php there.
    – CRAIG
    Nov 20 at 17:56










  • @Andreas I am encoding the result as JSON. Are you saying you can't echo JSON from admin-ajax.php? I thought that was the only acceptable way to utilize ajax in Wordpress? Am I missing something here?
    – CRAIG
    Nov 20 at 17:58


















  • '/wp-admin/admin-ajax.php' returns HTML and not JSON
    – Andreas
    Nov 20 at 17:52






  • 1




    In the Network tab, look at the Response tab to see what /wp-admin/admin-ajax.php?action=get_listing_names&name=name is returning.
    – aynber
    Nov 20 at 17:53










  • Not the issue, but pass you data like this data: {action: get_listing_names, name: name},
    – RiggsFolly
    Nov 20 at 17:54










  • @aynber. Hmm, actually, there isn't anything from admin-ajax.php there.
    – CRAIG
    Nov 20 at 17:56










  • @Andreas I am encoding the result as JSON. Are you saying you can't echo JSON from admin-ajax.php? I thought that was the only acceptable way to utilize ajax in Wordpress? Am I missing something here?
    – CRAIG
    Nov 20 at 17:58
















'/wp-admin/admin-ajax.php' returns HTML and not JSON
– Andreas
Nov 20 at 17:52




'/wp-admin/admin-ajax.php' returns HTML and not JSON
– Andreas
Nov 20 at 17:52




1




1




In the Network tab, look at the Response tab to see what /wp-admin/admin-ajax.php?action=get_listing_names&name=name is returning.
– aynber
Nov 20 at 17:53




In the Network tab, look at the Response tab to see what /wp-admin/admin-ajax.php?action=get_listing_names&name=name is returning.
– aynber
Nov 20 at 17:53












Not the issue, but pass you data like this data: {action: get_listing_names, name: name},
– RiggsFolly
Nov 20 at 17:54




Not the issue, but pass you data like this data: {action: get_listing_names, name: name},
– RiggsFolly
Nov 20 at 17:54












@aynber. Hmm, actually, there isn't anything from admin-ajax.php there.
– CRAIG
Nov 20 at 17:56




@aynber. Hmm, actually, there isn't anything from admin-ajax.php there.
– CRAIG
Nov 20 at 17:56












@Andreas I am encoding the result as JSON. Are you saying you can't echo JSON from admin-ajax.php? I thought that was the only acceptable way to utilize ajax in Wordpress? Am I missing something here?
– CRAIG
Nov 20 at 17:58




@Andreas I am encoding the result as JSON. Are you saying you can't echo JSON from admin-ajax.php? I thought that was the only acceptable way to utilize ajax in Wordpress? Am I missing something here?
– CRAIG
Nov 20 at 17:58












1 Answer
1






active

oldest

votes


















2














Here is my code, Maybe this will help you.



JS file(global.js) Code



jQuery(document).ready(function($) {    

var searchRequest;
$('#autocomplete-id').autoComplete({
minChars: 2,
source: function(name, result){
try { searchRequest.abort(); } catch(e){}
searchRequest = $.post(global.ajax, { name: name, action: 'get_listing_names' }, function(res) {
result(res.data);
});
}
});


});



Functions.php File code



    <?php
/**
* Enqueue scripts and styles.
*
* @since 1.0.0
*/
function ja_global_enqueues() {
wp_enqueue_style(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.css',
array(),
'1.0.7'
);
wp_enqueue_script(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.min.js',
array( 'jquery' ),
'1.0.7',
true
);
wp_enqueue_script(
'global',
get_template_directory_uri() . '/js/global.js',
array( 'jquery' ),
'1.0.0',
true
);
wp_localize_script(
'global',
'global',
array(
'ajax' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {
global $wpdb; //get access to the WordPress database object variable

//get names of all businesses
$name = $wpdb->esc_like(stripslashes($_POST['name'])).'%'; //escape for use in LIKE statement
$sql = "select post_title
from $wpdb->posts
where post_status='publish' LIMIT 10";

$sql = $wpdb->prepare($sql, $name);

$results = $wpdb->get_results($sql);

//copy the business titles to a simple array
$titles = array();

foreach( $results as $r ){
$titles = addslashes($r->post_title);
}

wp_send_json_success( $titles );

}





share|improve this answer





















  • Hey @Jaydeep. Thanks for answering! What is the Global.js file? I don't have this file. (At least I don't think I do?)
    – CRAIG
    Nov 23 at 4:53










  • Hello @CRAIG, You have to use your jQuery Code in some JS file and have to put localize your js go use the AJAX in Wordpress.
    – Jaydeep Jagani
    Nov 23 at 11:53










  • Woo hoo! Thanks so much @Jaydeep! So, the critical missing step for me was putting the jquery in that separate file. Once i did that I was able to get the rest sorted out. Thanks so much for the help.
    – CRAIG
    Nov 24 at 4:08










  • One final question. How could I return separate data for the input field and a separate value (is number) for the value field?
    – CRAIG
    Nov 24 at 17:49










  • Can you Please elaborate more.. !!
    – Jaydeep Jagani
    Nov 26 at 4:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398733%2funexpected-token-in-json-error-with-wordpress-autocomplete-jquery%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Here is my code, Maybe this will help you.



JS file(global.js) Code



jQuery(document).ready(function($) {    

var searchRequest;
$('#autocomplete-id').autoComplete({
minChars: 2,
source: function(name, result){
try { searchRequest.abort(); } catch(e){}
searchRequest = $.post(global.ajax, { name: name, action: 'get_listing_names' }, function(res) {
result(res.data);
});
}
});


});



Functions.php File code



    <?php
/**
* Enqueue scripts and styles.
*
* @since 1.0.0
*/
function ja_global_enqueues() {
wp_enqueue_style(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.css',
array(),
'1.0.7'
);
wp_enqueue_script(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.min.js',
array( 'jquery' ),
'1.0.7',
true
);
wp_enqueue_script(
'global',
get_template_directory_uri() . '/js/global.js',
array( 'jquery' ),
'1.0.0',
true
);
wp_localize_script(
'global',
'global',
array(
'ajax' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {
global $wpdb; //get access to the WordPress database object variable

//get names of all businesses
$name = $wpdb->esc_like(stripslashes($_POST['name'])).'%'; //escape for use in LIKE statement
$sql = "select post_title
from $wpdb->posts
where post_status='publish' LIMIT 10";

$sql = $wpdb->prepare($sql, $name);

$results = $wpdb->get_results($sql);

//copy the business titles to a simple array
$titles = array();

foreach( $results as $r ){
$titles = addslashes($r->post_title);
}

wp_send_json_success( $titles );

}





share|improve this answer





















  • Hey @Jaydeep. Thanks for answering! What is the Global.js file? I don't have this file. (At least I don't think I do?)
    – CRAIG
    Nov 23 at 4:53










  • Hello @CRAIG, You have to use your jQuery Code in some JS file and have to put localize your js go use the AJAX in Wordpress.
    – Jaydeep Jagani
    Nov 23 at 11:53










  • Woo hoo! Thanks so much @Jaydeep! So, the critical missing step for me was putting the jquery in that separate file. Once i did that I was able to get the rest sorted out. Thanks so much for the help.
    – CRAIG
    Nov 24 at 4:08










  • One final question. How could I return separate data for the input field and a separate value (is number) for the value field?
    – CRAIG
    Nov 24 at 17:49










  • Can you Please elaborate more.. !!
    – Jaydeep Jagani
    Nov 26 at 4:12
















2














Here is my code, Maybe this will help you.



JS file(global.js) Code



jQuery(document).ready(function($) {    

var searchRequest;
$('#autocomplete-id').autoComplete({
minChars: 2,
source: function(name, result){
try { searchRequest.abort(); } catch(e){}
searchRequest = $.post(global.ajax, { name: name, action: 'get_listing_names' }, function(res) {
result(res.data);
});
}
});


});



Functions.php File code



    <?php
/**
* Enqueue scripts and styles.
*
* @since 1.0.0
*/
function ja_global_enqueues() {
wp_enqueue_style(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.css',
array(),
'1.0.7'
);
wp_enqueue_script(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.min.js',
array( 'jquery' ),
'1.0.7',
true
);
wp_enqueue_script(
'global',
get_template_directory_uri() . '/js/global.js',
array( 'jquery' ),
'1.0.0',
true
);
wp_localize_script(
'global',
'global',
array(
'ajax' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {
global $wpdb; //get access to the WordPress database object variable

//get names of all businesses
$name = $wpdb->esc_like(stripslashes($_POST['name'])).'%'; //escape for use in LIKE statement
$sql = "select post_title
from $wpdb->posts
where post_status='publish' LIMIT 10";

$sql = $wpdb->prepare($sql, $name);

$results = $wpdb->get_results($sql);

//copy the business titles to a simple array
$titles = array();

foreach( $results as $r ){
$titles = addslashes($r->post_title);
}

wp_send_json_success( $titles );

}





share|improve this answer





















  • Hey @Jaydeep. Thanks for answering! What is the Global.js file? I don't have this file. (At least I don't think I do?)
    – CRAIG
    Nov 23 at 4:53










  • Hello @CRAIG, You have to use your jQuery Code in some JS file and have to put localize your js go use the AJAX in Wordpress.
    – Jaydeep Jagani
    Nov 23 at 11:53










  • Woo hoo! Thanks so much @Jaydeep! So, the critical missing step for me was putting the jquery in that separate file. Once i did that I was able to get the rest sorted out. Thanks so much for the help.
    – CRAIG
    Nov 24 at 4:08










  • One final question. How could I return separate data for the input field and a separate value (is number) for the value field?
    – CRAIG
    Nov 24 at 17:49










  • Can you Please elaborate more.. !!
    – Jaydeep Jagani
    Nov 26 at 4:12














2












2








2






Here is my code, Maybe this will help you.



JS file(global.js) Code



jQuery(document).ready(function($) {    

var searchRequest;
$('#autocomplete-id').autoComplete({
minChars: 2,
source: function(name, result){
try { searchRequest.abort(); } catch(e){}
searchRequest = $.post(global.ajax, { name: name, action: 'get_listing_names' }, function(res) {
result(res.data);
});
}
});


});



Functions.php File code



    <?php
/**
* Enqueue scripts and styles.
*
* @since 1.0.0
*/
function ja_global_enqueues() {
wp_enqueue_style(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.css',
array(),
'1.0.7'
);
wp_enqueue_script(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.min.js',
array( 'jquery' ),
'1.0.7',
true
);
wp_enqueue_script(
'global',
get_template_directory_uri() . '/js/global.js',
array( 'jquery' ),
'1.0.0',
true
);
wp_localize_script(
'global',
'global',
array(
'ajax' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {
global $wpdb; //get access to the WordPress database object variable

//get names of all businesses
$name = $wpdb->esc_like(stripslashes($_POST['name'])).'%'; //escape for use in LIKE statement
$sql = "select post_title
from $wpdb->posts
where post_status='publish' LIMIT 10";

$sql = $wpdb->prepare($sql, $name);

$results = $wpdb->get_results($sql);

//copy the business titles to a simple array
$titles = array();

foreach( $results as $r ){
$titles = addslashes($r->post_title);
}

wp_send_json_success( $titles );

}





share|improve this answer












Here is my code, Maybe this will help you.



JS file(global.js) Code



jQuery(document).ready(function($) {    

var searchRequest;
$('#autocomplete-id').autoComplete({
minChars: 2,
source: function(name, result){
try { searchRequest.abort(); } catch(e){}
searchRequest = $.post(global.ajax, { name: name, action: 'get_listing_names' }, function(res) {
result(res.data);
});
}
});


});



Functions.php File code



    <?php
/**
* Enqueue scripts and styles.
*
* @since 1.0.0
*/
function ja_global_enqueues() {
wp_enqueue_style(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.css',
array(),
'1.0.7'
);
wp_enqueue_script(
'jquery-auto-complete',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.min.js',
array( 'jquery' ),
'1.0.7',
true
);
wp_enqueue_script(
'global',
get_template_directory_uri() . '/js/global.js',
array( 'jquery' ),
'1.0.0',
true
);
wp_localize_script(
'global',
'global',
array(
'ajax' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );


add_action('wp_ajax_nopriv_get_listing_names', 'ajax_listings');
add_action('wp_ajax_get_listing_names', 'ajax_listings');


function ajax_listings() {
global $wpdb; //get access to the WordPress database object variable

//get names of all businesses
$name = $wpdb->esc_like(stripslashes($_POST['name'])).'%'; //escape for use in LIKE statement
$sql = "select post_title
from $wpdb->posts
where post_status='publish' LIMIT 10";

$sql = $wpdb->prepare($sql, $name);

$results = $wpdb->get_results($sql);

//copy the business titles to a simple array
$titles = array();

foreach( $results as $r ){
$titles = addslashes($r->post_title);
}

wp_send_json_success( $titles );

}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 8:29









Jaydeep Jagani

541




541












  • Hey @Jaydeep. Thanks for answering! What is the Global.js file? I don't have this file. (At least I don't think I do?)
    – CRAIG
    Nov 23 at 4:53










  • Hello @CRAIG, You have to use your jQuery Code in some JS file and have to put localize your js go use the AJAX in Wordpress.
    – Jaydeep Jagani
    Nov 23 at 11:53










  • Woo hoo! Thanks so much @Jaydeep! So, the critical missing step for me was putting the jquery in that separate file. Once i did that I was able to get the rest sorted out. Thanks so much for the help.
    – CRAIG
    Nov 24 at 4:08










  • One final question. How could I return separate data for the input field and a separate value (is number) for the value field?
    – CRAIG
    Nov 24 at 17:49










  • Can you Please elaborate more.. !!
    – Jaydeep Jagani
    Nov 26 at 4:12


















  • Hey @Jaydeep. Thanks for answering! What is the Global.js file? I don't have this file. (At least I don't think I do?)
    – CRAIG
    Nov 23 at 4:53










  • Hello @CRAIG, You have to use your jQuery Code in some JS file and have to put localize your js go use the AJAX in Wordpress.
    – Jaydeep Jagani
    Nov 23 at 11:53










  • Woo hoo! Thanks so much @Jaydeep! So, the critical missing step for me was putting the jquery in that separate file. Once i did that I was able to get the rest sorted out. Thanks so much for the help.
    – CRAIG
    Nov 24 at 4:08










  • One final question. How could I return separate data for the input field and a separate value (is number) for the value field?
    – CRAIG
    Nov 24 at 17:49










  • Can you Please elaborate more.. !!
    – Jaydeep Jagani
    Nov 26 at 4:12
















Hey @Jaydeep. Thanks for answering! What is the Global.js file? I don't have this file. (At least I don't think I do?)
– CRAIG
Nov 23 at 4:53




Hey @Jaydeep. Thanks for answering! What is the Global.js file? I don't have this file. (At least I don't think I do?)
– CRAIG
Nov 23 at 4:53












Hello @CRAIG, You have to use your jQuery Code in some JS file and have to put localize your js go use the AJAX in Wordpress.
– Jaydeep Jagani
Nov 23 at 11:53




Hello @CRAIG, You have to use your jQuery Code in some JS file and have to put localize your js go use the AJAX in Wordpress.
– Jaydeep Jagani
Nov 23 at 11:53












Woo hoo! Thanks so much @Jaydeep! So, the critical missing step for me was putting the jquery in that separate file. Once i did that I was able to get the rest sorted out. Thanks so much for the help.
– CRAIG
Nov 24 at 4:08




Woo hoo! Thanks so much @Jaydeep! So, the critical missing step for me was putting the jquery in that separate file. Once i did that I was able to get the rest sorted out. Thanks so much for the help.
– CRAIG
Nov 24 at 4:08












One final question. How could I return separate data for the input field and a separate value (is number) for the value field?
– CRAIG
Nov 24 at 17:49




One final question. How could I return separate data for the input field and a separate value (is number) for the value field?
– CRAIG
Nov 24 at 17:49












Can you Please elaborate more.. !!
– Jaydeep Jagani
Nov 26 at 4:12




Can you Please elaborate more.. !!
– Jaydeep Jagani
Nov 26 at 4:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398733%2funexpected-token-in-json-error-with-wordpress-autocomplete-jquery%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen