0
<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

5
  • salesChartData.datasets[0].label, salesChartData.datasets[0].fillColor and so on. obviously you can iterate over the key/value pairs Commented Sep 24, 2019 at 6:49
  • Isn't jQuery written in JavaScript? Commented Sep 24, 2019 at 6:55
  • I am getting data from database in jquery function ,so how can i get this value in javascript Commented Sep 24, 2019 at 8:13
  • I am getting every thing fine,only the problem is i can not show my data getting from database ,in daset you can see i have mention "here i want my data" in that place i want my database data.@caramba Commented Sep 24, 2019 at 8:16
  • both are written in diffrent script @Harun Yilmaz Commented Sep 24, 2019 at 10:09

5 Answers 5

1

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;
});
Sign up to request clarification or add additional context in comments.

6 Comments

thank u for your answer,but how can i get my jquery data here
I am getting every thing fine,only the problem is i can not show my data getting from database ,in dataset you can see i have mention "here i want my data" in that place i want my database data
Do you need the key/value pairs from your received data to append to datasets also as key/value paired? So no data attribute?
i am getting my data in this format 10,45,21,10,5,45 and exact data i want to push there
Is 10,45,21,10,5,45 a string or an array?
|
0

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

2 Comments

how it will interact with the js without using any id or class,both are written in different script
The "both are written in different script" part wasn't in the question when i sent you my answer. In any case, javascript vars are global, so you just need to make sure that the script that creates the var "salesChartData" is called/included/defined before the ajax call.
0

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

7 Comments

how it will interact with the js without using any id or class,both are written in different script
@Zubair: For accing from different files, you need to add the value in web storage.
everywhere i got that they sending value in jsp's div by class or by id but in my case i am not using any jsp
@Zubair: Can you please elaborate. I didn't get you much.
@Zubair: You can use by storing a hidden input text also.
|
0

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]
    }
  ]
};

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.