从Javascript中的代码访问数据

时间:2022-08-25 21:53:06

I am using devexpress JS Charts which they accept data in a format of:

我正在使用devexpress JS Charts,他们接受以下格式的数据:

[{ category: 'Oceania', value: 35 },{ category: 'Europe', value: 728 }]

I am able to get the exact same format in my back end code by converting a DataTable to a Json. (I am getting the datatable after i run a couple of queries based of the user's selection)

通过将DataTable转换为Json,我能够在后端代码中获得完全相同的格式。 (根据用户的选择运行几个查询后,我得到了数据表)

Everything was working fine untill i decided to use an UpdatePanel to avoid the postbacks in the page every time the user was selecting an option (Please note that i am not experienced in something like this). Now ,though, i realized that i can't call a non static method with javascript in order to get the data. What i thought i could do is every time i have an ajax postback, i would run a method and insert into a hidden field the data and then grab it with javascript and populate my charts. When i am doing that it's not working as the data is stored as string... I tried a couple of things around it but i simple can't make this work...

一切都工作正常,直到我决定使用UpdatePanel,以避免每次用户选择一个选项时页面中的回发(请注意,我没有这样的经验)。但是,现在,我意识到我不能用javascript调用非静态方法来获取数据。我认为我能做的是每次我有一个ajax回发,我会运行一个方法并插入一个隐藏的字段数据,然后用javascript抓住它并填充我的图表。当我这样做它没有工作,因为数据存储为字符串...我尝试了一些事情,但我简单无法使这项工作......

I was grabbing the data before (with post back)

我之前抓住了数据(回帖后)

var data= <%=GetData()%>

The GetData() is a C# method that returns a string

GetData()是一个返回字符串的C#方法

private string GetJsonFormat(DataTable table)

In order to populate the charts i must use

为了填充我必须使用的图表

$("#divID").dxPieChart({dataSource: data});

If i input the data into an input field and grab it like this

如果我将数据输入到输入字段并像这样抓住它

var data= $('#inputID').val();

It doesn't work because of the string quotes... I even tried to remove the quotes like this

由于字符串引号,它不起作用......我甚至试图删除这样的引号

data.substring(1,data.length()-1)

but didn't work either...

但也没有工作......

Can someone see an alternative way around this or a fix for this?

有人可以看到另一种解决方法或修复此问题吗?

1 个解决方案

#1


1  

The correct approach would be to use AJAX. For example, jQuery has built-in functions for getting JSON content from the server.

正确的方法是使用AJAX。例如,jQuery具有用于从服务器获取JSON内容的内置函数。

http://api.jquery.com/jQuery.getJSON/

http://api.jquery.com/jQuery.getJSON/

It's extremely simple. The easiest approach is to get your button (or whatever triggers the event) to simply call:

这非常简单。最简单的方法是让你的按钮(或触发事件的任何东西)简单地调用:

$.getJSON('url/to/json/content', function(data) {
    // data is the returned JSON object so use it as you like
});

#1


1  

The correct approach would be to use AJAX. For example, jQuery has built-in functions for getting JSON content from the server.

正确的方法是使用AJAX。例如,jQuery具有用于从服务器获取JSON内容的内置函数。

http://api.jquery.com/jQuery.getJSON/

http://api.jquery.com/jQuery.getJSON/

It's extremely simple. The easiest approach is to get your button (or whatever triggers the event) to simply call:

这非常简单。最简单的方法是让你的按钮(或触发事件的任何东西)简单地调用:

$.getJSON('url/to/json/content', function(data) {
    // data is the returned JSON object so use it as you like
});