0

The file is within .php ext and it's in the WordPress template page. When I put variable to get URI to $.post request it just doesn't work. However, if I replace vurl with absolute url, it totally works. The code looks okay to me or am I missing something? If someone can shed light on this.

  jQuery(document).ready(function() {
    $('#player').on('input', function() {
      var searchKeyword = $(this).val();
        if (searchKeyword.length >= 2) {

        var vurl = '<?= get_template_directory_uri(); ?>';
         $.post('"+vurl+"/others/goals.php', { keywords: searchKeyword }, function(data)

                 {
                 $('ul#cf').empty()
                $.each(data, function(ding, dong) {
               $("ul#cf").append('<a><li id="'+ dong.id +'"> ' + dong.title+ '</li></a>');
               $('ul#cf').on('click','li',function(){
               $('#player').val(this.id);
                });
               });
             }, "json");
            }
        });
    });

1 Answer 1

2

Correct code :

$.post(vurl+'/others/goals.php',  ...
Sign up to request clarification or add additional context in comments.

4 Comments

Correct, thought he was missing the echo before the function call for the URI but he's used the shorthand "<?="
For some strange reason it continues to not work. Replacing var with url works normally. Very strange!
Maybe you have two slashes in your url (if vurl ended by /). Insert console.log(vurl +'/others/goals.php'); just before $.post(... and check the browser console result.
Appreciate your suggestion. URL path seems to be working okay but it's showing GET 404 (no found) to this file wp-includes/js/jquery/jquery.js?ver=1.12.4

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.