Sunday, August 7, 2022
HomeWordPress DevelopmentWhy Wordpress AJAX returns undefined nevertheless it really works wonderful once I...

Why WordPress AJAX returns undefined nevertheless it really works wonderful once I add static url as a substitute of dynamic operate?


Code from capabilities.php

operate university_files()
{
   wp_localize_script('main-university-js', 'universityData', array(
        'root-url' => get_site_url()
    ));
};

add_action('wp_enqueue_scripts', 'university_files');

Code from JS

getResults() {
    $.getJSON(
      universityData.root_url +
        "/wp-json/wp/v2/posts?search=" +
        this.searchField.val(),
      (posts) => {
        this.resultsDiv.html(`
        <h2 class ="search-overlay__section-title">Common Data</h2>
          ${
            posts.size
              ? `<ul class="link-list min-list">`
              : `No Common Data Marched your search`
          }
            ${posts
              .map(
                (merchandise) =>
                  `<li><a href="${merchandise.hyperlink}">${merchandise.title.rendered}</a></li>`
              )
              .be a part of("")}
          ${posts.size ? `</ul>` : ""}
        
        `);
        this.isSpinnerVisible = false;
      }
    );
  }

Image from inspect element where the error can be seen

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments