0

json response is received and shown to users in and it works without issue but if i want to show javascript alert it is not working.

during initial pageload that var is empty so i dont expect that to process so i have disabled javascript processing if var is empty but when the form is uploaded and json data is returned then its not empty and correctly formatted json data which is visible under value { x: 3, y: 33.6 },{ x: 4, y: 92.6 },{ x: 5, y: 121.2 } but when i try to alert the same thing with javascript it does not worked i always get empty message never gets done message.

    <script>   
$(document).ready(function() {
        var ndhtml =[];

    if(ndhtml.length==0)
    {
    alert('empty string')   
    }

    else

    {


var kkpppf=$("#chek").html(ndhtml);
alert('done');
    }
});

</script>

any advise how to update json data here will be great. first js file is external file and code of that file is

$(document).ready(function() {
    (function() {
        $('#upload-form2').ajaxForm({
            dataType: 'json',
             success: function(data) {
                var ndhtml = '',
                    downlo;
                for (var i = 0; i < data.length; i++) {
                    downlo = data[i];
                    ndhtml += '' + downlo.ndchart + '';
                }
              $('#chek').html(ndhtml);
               }
        })
    })();
  });
hareom
  • 19
  • 5
  • It looks like the first script sets ndhtml and the second resets it. You need to remove var ndhtml =[]; – SpaceCowboy Jun 21 '17 at 08:13
  • @SpaceCowboy then chrome consol gives error. undefined ndhtml – hareom Jun 21 '17 at 08:15
  • Make sure you have the scope right. This might help: https://stackoverflow.com/questions/2932782/global-variables-in-javascript-across-multiple-files – SpaceCowboy Jun 21 '17 at 08:17
  • @SpaceCowboy seems my problem. let me try if that worked – hareom Jun 21 '17 at 08:20
  • You need to add the alert inside the `success:` callback - it's always going to be empty when the page first loads as it hasn't had time to set it yet. https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323 – freedomn-m Jun 21 '17 at 08:33
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – freedomn-m Jun 21 '17 at 08:33

1 Answers1

0

Please try below mentioned code, it will defiantly helps you,

var ndhtml = {}; //Default ndhtml empty object

$(document).ready(function($) {
    //updateHtml(); //call update function

    $('#upload-form2').ajaxForm({
        dataType: 'json',
        success: function(data) {
            ndhtml = data; //Initialize ajax success data to ndhtml
            updateHtml(); //again call same function to set HTML
        }
    });
});



var updateHtml = function(){

    if(ndhtml.length > 0) //Check the ndhtml variable length
    {   
        var html = "";
        ndhtml.each(function(index, el) { //looping each element of ndhtml
            html += '' + el.ndchart + ''; // Append ndchart varibale to html variable
        });

        $('#chek').html(html); //Prepared html append to selected element
    }
    else
    {
        alert("Empty"); //If ndhtml is empty than show alert box
    }
}
Binary Brackets
  • 494
  • 1
  • 4
  • 12
  • I have update my code, Please try again, Your all required code is here, it's depend you how to implement as per your requirement. – Binary Brackets Jun 21 '17 at 08:36
  • first js file is external file and you are clubbing code of both files in one file which is not allowed at our end due to some restrictions – hareom Jun 21 '17 at 08:40
  • You just have to define `var ndhtml = {};` globally... so you will access from whatever file, and easily maintain your code – Binary Brackets Jun 21 '17 at 08:42