$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>ajax - Javascript output now showing in custom widget|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

ajax - Javascript output now showing in custom widget

matteradmin9PV0评论

I have an issue with Javascript that I created for my custom widget.

Here is the code:

    var wrapper = $('#wrapper'), container;

    $.ajax({
    method: "POST",
    url: "",
    contentType: "application/json",
    headers: {
    Authorization: "bearer ******"
  },
 data: JSON.stringify({
    query: "query  { allProducts { id title } }"
  })
}).done(function(data) {

for( var key in data ) {

     for (var i = 0; i<data[key].allProducts.length; i++)
     {
         console.log(data[key].allProducts[i]);
         container = $('<div id="data" class="container"></div>');
         wrapper.appendTo(container);
         container.appendTo('<input type=checkbox name="id" value=' + data[key].allProducts[i].id + '>' + data[key].allProducts[i].title  );             
     }   

}

});

Here is how I'm calling the Jquery in my functions.php file for the template:

  function jquery_import() {

          wp_deregister_script('jquery');
          wp_register_script('jquery',.3.1/jquery.min.js', array(), '3.3.1', true);  
           wp_enqueue_script('jquery');

     }
     add_action( 'wp_enqueue_scripts', 'jquery_import' );

Here is how I'm calling my custom javascript file:

     function custom_wp_enqueue_scripts() {

        wp_register_script( 'custom', get_template_directory_uri() . '/assets/js/custom.js', array( 'jquery' ), NULL, false );
         wp_enqueue_script( 'custom' );

       }    
      add_action('wp_enqueue_scripts', 'custom_wp_enqueue_scripts');

Finally, in my widget here is the following div statement:

      <div id="wrapper"> </div>

Based on my javascript, data is coming in, however, the information is not displayed in my widget. Why it isn't showing in my widget?

Thank you, Kevin Davis

I have an issue with Javascript that I created for my custom widget.

Here is the code:

    var wrapper = $('#wrapper'), container;

    $.ajax({
    method: "POST",
    url: "https://api.graphcms/simple/v1/SampleAPI",
    contentType: "application/json",
    headers: {
    Authorization: "bearer ******"
  },
 data: JSON.stringify({
    query: "query  { allProducts { id title } }"
  })
}).done(function(data) {

for( var key in data ) {

     for (var i = 0; i<data[key].allProducts.length; i++)
     {
         console.log(data[key].allProducts[i]);
         container = $('<div id="data" class="container"></div>');
         wrapper.appendTo(container);
         container.appendTo('<input type=checkbox name="id" value=' + data[key].allProducts[i].id + '>' + data[key].allProducts[i].title  );             
     }   

}

});

Here is how I'm calling the Jquery in my functions.php file for the template:

  function jquery_import() {

          wp_deregister_script('jquery');
          wp_register_script('jquery',https://ajax.googleapis/ajax/libs/3.3.1/jquery.min.js', array(), '3.3.1', true);  
           wp_enqueue_script('jquery');

     }
     add_action( 'wp_enqueue_scripts', 'jquery_import' );

Here is how I'm calling my custom javascript file:

     function custom_wp_enqueue_scripts() {

        wp_register_script( 'custom', get_template_directory_uri() . '/assets/js/custom.js', array( 'jquery' ), NULL, false );
         wp_enqueue_script( 'custom' );

       }    
      add_action('wp_enqueue_scripts', 'custom_wp_enqueue_scripts');

Finally, in my widget here is the following div statement:

      <div id="wrapper"> </div>

Based on my javascript, data is coming in, however, the information is not displayed in my widget. Why it isn't showing in my widget?

Thank you, Kevin Davis

Share Improve this question asked Jan 4, 2019 at 16:33 Kevin DavisKevin Davis 1
Add a comment  | 

1 Answer 1

Reset to default 0

Try changing...

var wrapper = $('#wrapper'), container;

To just...

var wrapper = $('#wrapper');

then add var in front of container

var container = $('<div id="data" class="container"></div>');
Post a comment

comment list (0)

  1. No comments so far