0

I have the following script. As shown when you run the snippet, console.log doesn't work. How can I enable it?

datain = {
        "threshold": 1.5,
        "sample_response_score": 0.439,
        //"celltype_response_thres": 0.064,
        "celltype_response_thres": 'foo',
        "histograms": [
            {
                "sample": "Sample1",
                "nof_genes": 26,
                "values": [
                    {
                        "score": 0.042924727328924939,
                        "celltype": "Bcells"
                    },
                    {
                        "score": 0.073045907156188195,
                        "celltype": "DendriticCells"
                    }]
                  
                }]}
   
           



jQuery(function ($) {
     $.get(datain, function (data) {
         console.log(JSON.stringify(data));
       
       });
  });
            
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Phil
  • 157,677
  • 23
  • 242
  • 245
neversaint
  • 60,904
  • 137
  • 310
  • 477

2 Answers2

4

console.log is working fine-- you're just using .get incorrectly, so the callback is never firing. See my snippet.

See the documentation for jQuery.get -- it is expecting a URL to fetch data, or a configuration object with a url property. You seem to be attempting to just pass it the result object you want to be fetching. (Thanks Phil for the correction)

console.log('it works fine');
datain = {
        "threshold": 1.5,
        "sample_response_score": 0.439,
        //"celltype_response_thres": 0.064,
        "celltype_response_thres": 'foo',
        "histograms": [
            {
                "sample": "Sample1",
                "nof_genes": 26,
                "values": [
                    {
                        "score": 0.042924727328924939,
                        "celltype": "Bcells"
                    },
                    {
                        "score": 0.073045907156188195,
                        "celltype": "DendriticCells"
                    }]
                  
                }]}
   
           



jQuery(function ($) {
     $.get(datain, function (data) {
         console.log(JSON.stringify(data));
       
       });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
  • FYI, `$.get` can accept an object but it expects it to have an `url` property. See https://api.jquery.com/jquery.get/#jQuery-get-settings – Phil Oct 21 '16 at 03:21
  • @Phil -- fixed-- thanks! – Alexander Nied Oct 21 '16 at 03:23
  • @Phil only as of v 1.12/2.2 - so 2.1.1 wont do that – Jaromanda X Oct 21 '16 at 03:24
  • @JaromandaX how does that `1.12/2.2` thing work? Did they remove it at some time then add it back? – Phil Oct 21 '16 at 03:25
  • He is right. You are a bit confused on SETTINGS, the object you are passing as datain is not a valid SETTINGS for $.get. Where should $.get() be pointing to fetch the data? Nowhere. So your callback never runs. – Ben Bozorg Oct 21 '16 at 03:26
  • 1
    parallel development of 1.x and 2.x - so, 2.2 was around the same time as 1.12 ... no backporting into 2.1 of the changes made in 2.2 ... damn confusing with 3 major lines of jquery!!! – Jaromanda X Oct 21 '16 at 03:26
0

get : Load data from the server using a HTTP GET request

Example:

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

datain = {
        "threshold": 1.5,
        "sample_response_score": 0.439,
        //"celltype_response_thres": 0.064,
        "celltype_response_thres": 'foo',
        "histograms": [
            {
                "sample": "Sample1",
                "nof_genes": 26,
                "values": [
                    {
                        "score": 0.042924727328924939,
                        "celltype": "Bcells"
                    },
                    {
                        "score": 0.073045907156188195,
                        "celltype": "DendriticCells"
                    }]
                  
                }]}
   
           



//jQuery(function ($) {
//    $.get(datain, function (data) {
         console.log(JSON.stringify(datain));
       
//       });
//  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Maybe you are not loading data from server properly using get.

You can also use ajax method for the same.

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

ajax reference: http://www.w3schools.com/jquery/jquery_ref_ajax.asp

arnabkaycee
  • 1,634
  • 13
  • 26