简体   繁体   中英

Showing data from Ajax response

I am trying to display my ajax response in a div, but I have no idea how to do that.

My code is the following:

<div class="divright">
   Div Content
</div>


< script >
  $(document).on("click", '.tribe-mini-calendar-day-link', function() {
    var date = $(this).data('day');
    $.ajax({

      type: "POST",
      url: TribeMiniCalendar.ajaxurl,
      data: {
        action: 'event_details',
        date: date
      },
      success: function(response) {
        console.log(response)
      },
      error: function(request, error) {
        alert("FOUT:" + error);
      }
    })
  })
</script>

If you click on .tribe-mini-calendar-day-link , the console says the following:

`{status: true, html: "<p><strong>Pratice: </strong> 2017-09-23 till 2017-09-24</p>"}`

Now my question is, how do I display that code in the divright ?

Your success function should be :

success: function (response) {
   if(response !== undefined && response.html !== undefined) {
      $(".divright").html(response.html);
   }
},

In success section of AJAX request, assuming you are parsing response right way.

var data = response.html;
if(data !== undefined)
    $(".divright").html(data);
else
    $(".divright").html("Error!");

Inside your success function you need to append html to the div. This can be done very easily using jquery as

if(response !== undefined && response.html !== undefined){
    $( ".divright" ).html(response.html);
}
else{
    $( ".divright" ).html("<div>Some Error Encountered</div>");
}

You should use Jquery .html response to update data in your div tag . such as $("#your_DIV_ID").html( data);

Here is your your complete code. `

<div id="my_Div_ID" class="divright"> Div Content </div>
    <script>
        $(document).on("click", '.tribe-mini-calendar-day-link', function() {
            var date = $(this).data('day');
            $.ajax({
                type: "POST",
                url: TribeMiniCalendar.ajaxurl,
                data: {
                    action: 'event_details',
                    date: date
                },
                success: function(response) {
                    $("#my_Div_ID").html(response);
                    console.log(response)
                },
                error: function(request, error) {
                    alert("FOUT:" + error);
                }
            })
        })
    </script>

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM