0

Can I get values from ajax into body html?? Here my example:

function check() {
        $.ajax({
            url : '/check',
            datatype : 'json',
            async : false,
            success : function(status) {
                console.log(status)
                #example: status = "abcdefgh"
            },
        });
    }

$(document).ready(function() { 
  setTimeout(check, 3000);
});

In html, I want to get status(="abcdefgh") in body like this:

<div class="medium-6 medium-centered row">
    <div class="medium-10 medium-centered columns">
       #I put status in to this: 
       <% @files = Dir.glob("app/assets/images/status/*.*") %>
    </div>
</div>

I tried to do this:

<p> <%= status %> </p>

but it doesn't work. So please! tell me how to get status in ajax call

mayoneQD
  • 61
  • 8
  • I don't know what for you have tried `<%= status %>` at all, but `async : false` is great evil, that's for sure. – Regent Mar 05 '15 at 10:28
  • @Regent, [everything has its purpose](http://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax). Unqualified statements like yours aren't helpful. – Simon MᶜKenzie Mar 05 '15 at 10:32
  • Sorry, I forgot to tell you i'm using ruby on rails. So, I've edited my question, please see it – mayoneQD Mar 05 '15 at 10:33
  • @SimonMᶜKenzie what _unqualified_ is in phrase that `async: false` is bad idea? – Regent Mar 05 '15 at 10:35
  • @Regent in that you're saying it's always a bad idea. It's not always a bad idea. It depends on the problem. – Simon MᶜKenzie Mar 05 '15 at 11:33
  • @SimonMᶜKenzie if you want users to suffer or simply don't care how it will work in production, then it's not bad idea for you, yes. Nevertheless, can you show an example when it is good (or at least neutral) idea? – Regent Mar 05 '15 at 11:40
  • @Regent, have a look at this one: http://stackoverflow.com/a/15289843/622391 – Simon MᶜKenzie Mar 05 '15 at 11:45
  • @SimonMᶜKenzie there is _which is not recomennded_ in this answer, there is _Setting this option to false (and thus making the call no longer asynchronous) is strongly discouraged, as it can cause the browser to become unresponsive._ in official docs. All these things hint that you **can** do this, but it is **not good at all**. – Regent Mar 05 '15 at 11:49
  • @Regent, [the docs](http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings) only say that use of `async:false` with **jqXHR (`$.Deferred`)** isn't recommended. They _do not_ say that `async:false` should not be used. Of course, it can make the browser unresponsive, and anyone using `async: false` certainly needs to be aware of this. This discussion has gotten a bit out of hand anyway - my original point was simply that you need to _qualify_ (i.e. explain _why_) when making broad statements. When you claim that an undeprecated API is bad, it's your responsibility to prove it! – Simon MᶜKenzie Mar 05 '15 at 22:12

4 Answers4

0

Use this

function check() {
        $.ajax({
            url : '/check',
            datatype : 'json',
            async : false,
            success : function(status) {
                  $(".medium-10 >p").html(status);
            },
        });
    }
Optimus
  • 2,200
  • 8
  • 37
  • 71
0

Use this you need find the class by using columns in div and place your call back html in p tag like $('.columns > p').html(status);

function check() {
        $.ajax({
            url : '/check',
            datatype : 'json',
            async : false,
            success : function(status) {
                console.log(status)
                #example: status = "abcdefgh"
            $('.columns > p').html(status); //use this
            },
        });
    }
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
0

in your success

 success : function(status) {
                console.log(status)
                #example: status = "abcdefgh"
                $('p').html(satus);
            },
Med.Amine.Touil
  • 1,225
  • 2
  • 11
  • 15
0
success : function(status) {
    console.log(status)
    #example: status = "abcdefgh"
    // way 1:
    $(".columns").prepend(status);
    // way 2:
    // if you wanted status inside a P tag
    $(".columns").prepend("<p>" + status + "</p>");
},

Way 1 will ouptput:

<div class="medium-10 medium-centered columns">
   abcdefgh 
   <% @files = Dir.glob("app/assets/images/status/*.*") %>
</div>

Way 2 will output:

<div class="medium-10 medium-centered columns">
   <p>abcdefgh</p>
   <% @files = Dir.glob("app/assets/images/status/*.*") %>
</div>
lshettyl
  • 8,166
  • 4
  • 25
  • 31
  • May be you right! but how do I put status in this line <% @files = Dir.glob("app/assets/images/status/*.*") %> – mayoneQD Mar 05 '15 at 11:04