Home > Back-end >  Display JSONP Array Using Jquery Loop
Display JSONP Array Using Jquery Loop

Time:03-12

I've dug through dozens of related questions, and tried to implement but just can't quite get it to come together. I'm certain it is just some painfully simple miss, since I'm a rookie.

Redacted URL as it has sensitive data, but it prints to the console with the full array, so the ajax call seems to work. Just can't quite wrap my mind around the final step of display the results in a DIV. From reading, it seems like a Loop is necessary but can't quite get there.

<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

<div id="jsonpResult"></div>

<script>
$(document).ready(function(){
        $.ajax({
            url: '**MyURL**',
            data: {check: 'one'},
            dataType: 'jsonp',
            jsonp: 'callback',
            jsonpCallback: 'jobs',
            success: function(result){
                console.log(result);
            }
        });
    });

  function jsonpCallback(data){
       for(var i = 0 ; i < data.length ; i  ){
            $('#jsonpResult').append(item.title "<br />");
       }
   }
</script>

CodePudding user response:

You might consider the following.

$(function() {
  $.ajax({
    url: '**MyURL**',
    data: {
      check: 'one'
    },
    dataType: 'jsonp',
    success: function(result) {
      console.log(result);
      $.each(result, function(i, item) {
        $("<div>").html(item.title).appendTo('#jsonpResult');
      });
    }
  });
});
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

<div id="jsonpResult"></div>

Without a Test URL, I am unable to test this code completely.

References:

  • Related