Zubair
Zubair

Reputation: 31

How to send data from jquery to javascript

<script>
     $.getJSON('chartState',{
                    stateCode : $(this).val(),
                        ajax : 'true'
                },
                function(data) {
                    alert("state data"+data);
                });
</script>

I have the value in data and want to show in javascript given below. The fields data is given want to push my state data there.

   <script>
        var salesChartData = {
            datasets: [{
                    data: ["here i want my data"]
                }]
        };
   </script>

Both are written in diffrent script

Upvotes: 0

Views: 427

Answers (5)

khoekman
khoekman

Reputation: 1153

As you mention that both parts of the script are in different tags you can solve the problem with a global, this is not recommended. The better solution would be to refactor the structure and not have multiple script tags. But if you have no control over this then you should do something like this:

<script>
     // No var used to make it global
     chart_state_data = false;
     $.getJSON('chartState',{
                    stateCode : $(this).val(),
                        ajax : 'true'
                },
                function(data) {
                    // the data is set to this variable on callback
                    chart_state_data = data
                });
</script>

And:

  <script>
        // chart_state_data contains data retrieved from ajax call or false
        var salesChartData = {
            datasets: [{
                    data: chart_state_data
                }]
        };
   </script>

Upvotes: 0

Zubair
Zubair

Reputation: 31

I have done with my self

$.getJSON('chartState',{
    stateCode : $(this).val(),
        ajax : 'true'
},
function(data) {
    var chr=data;
      var a=chr[0];var b=chr[1];var c=chr[2];var d=chr[3];
      var e=chr[4];var f=chr[5];var g=chr[6];

After that I have sended one by one data

var salesChartData = {
  datasets: [   
    {

      data                :  [g,f,e,d,c,b,a]
    }
  ]
};

Upvotes: 0

Nishal K.R
Nishal K.R

Reputation: 1130

if you need to show the ajax result in a variable salesChartData, you can try this salesChartData.datasets[0].data[0] = "new data"

salesChartData is a JSON object with key datasets contains an array of JSON objects.

So if salesChartData is declared globally, then you can replace in the success of the ajax

Here below, it done using web storage. This is used to access from different file.

// File 1
var salesChartData = {
    datasets: [{
        data: ["here i want my data"]
    }]
};
localStorage.setItem("salesChart", JSON.stringify(salesChartData));

//-----------------------------------------------------------------------

// File 2

var salesChartData = JSON.parse(localStorage.getItem("salesChart"));

// ajax call
$.getJSON('chartState', {
        stateCode: $(this).val(),
        ajax: 'true'
    },
    function (data) {
        alert("state data" + data);
        salesChartData.datasets[0].data[0] = data // "new data"
    });

Hope this will work.

Thank You

Upvotes: 0

tom
tom

Reputation: 10601

datasets is an array with an object on index 0. So to define or redeclare the data property in there the syntax is

salesChartData.datasets[0].data = data;

Use it in your callback function:

function(data) {
   salesChartData.datasets[0].data = data;
});

Upvotes: 1

Not sure if I understand correctly, is this what you need?

var salesChartData = {
  datasets: [
    {
      data :  {}
    }
  ]
};


$.getJSON('chartState',{
  stateCode : $(this).val(),
  ajax : 'true'
},
function(data) {
  salesChartData.datasets[0].data = data;
});

Just set the data after receiving it

Upvotes: 0

Related Questions