2
Hello, I’m starting to use ajax and the Google API (Chart) to create charts.
I use a Generic Handler I called 'Collaboratorsempresa.ashx', in it I make a query in the database and return a string this way;
context.Response.Write(valores.ToString());
The data I receive comes to Ajax as a string (in the correct format of the parameter I should pass) as follows;
My question: 'Is it possible to convert this string to a valid array or parameter so that my call to the Google API works? 'cause the way it is now I get an error message saying: 'Javascript Runtime Error: Not an array'
Ajax function:
< script type = "text/javascript" >
google.load("visualization", "1", {
packages: ["corechart"]
});
$(document).ready(function() {
var urlH = "GenericHandler/ColaboradoresEmpresa.ashx";
$.ajax({
url: urlH,
type: "POST",
data: {},
async: true,
success: function(Valores) {
alert(Valores);
var data = google.visualization.arrayToDataTable(Valores);
var options = {
title: '',
is3D: false
};
var chart = new google.visualization.PieChart(document.getElementById('Empresa'));
chart.draw(data, options);
},
error: function(data) {
alert("ERRO: " + data.status);
},
timeout: 15000
});
}); < /script>
File code . ASHX which returns the string pro Ajax;
public void ProcessRequest(HttpContext context) { SqlCommand comando = new SqlCommand(); comando.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NOMEDB"].ConnectionString); comando.CommandText = "SELECT/CONSULTA NO BANCO DE DADOS"; comando.Connection.Open(); string valores = "[['Empresa_', 'Colaboradores'], "; SqlDataReader dr = comando.ExecuteReader(); while (dr.Read()) { if (dr["NOMEFANTASIA"] != DBNull.Value) { valores += "['" + dr["NOMEFANTASIA"].ToString() + "',"; } if (dr["QTD"] != DBNull.Value) { valores += dr["QTD"].ToString() + "],"; } } if (valores.Length > 0) { valores = valores.Substring(0, valores.Length - 1) + ", ]"; } comando.Connection.Close(); context.Response.Write(valores.ToString()); }
a question, the variable
valores
server-side, is an array?– Enzo Tiezzi
@Enzotiezzi is a string in the format: "['Empresa_', 'Contributors'], ['Cleber', 27000], ['Jonathan', 27000], ['Leonardo', 27000], ]". I don’t know how to convert her.
– Cleber Teixeira
How do you get the value of this
String
on your server?– Enzo Tiezzi
@Enzotiezzi I make a query in a database through a file . ashx, concateto and return a string in the above format and return it pro ajax with a
context.Response.Write(valores.ToString());
but you can also read this ascontext.Response.Write("[['Empresa_', 'Colaboradores'], ['Cleber', 27000], ['Jonathan', 27000], ['Leonardo', 27000], ]");
– Cleber Teixeira
then, if you can, put this part that you said concatenates, because then we can try to solve in a way that already comes the correct information, or at least the most correct possible.
– Enzo Tiezzi
@Enzotiezzi posted the code, also tried to create an array with Ajax but it accuses "0x800a03f6 - Error in Javascript execution time: Invalid character". Code: var vet = JSON.parse(Values); var data = google.visualization.arrayToDataTable(vet);
– Cleber Teixeira
All right, now let’s wrap this up, it has to be fixed by the server side itself, let’s go
– Enzo Tiezzi
let’s make a try, the variable
valores
will become aList<string> valores = new List<string>();
and you already put there the value you want in her,valores.Add("['Empresa_', 'Colaboradores']");
, this is the first step– Enzo Tiezzi
@Enzotiezzi left the code like this:
comando.Connection.Open();List<string> valores = new List<string>();valores.Add("[['Empresa_', 'Colaboradores']");SqlDataReader dr = comando.ExecuteReader();while (dr.Read())
 {if (dr["NOMEFANTASIA"] != DBNull.Value && dr["QTD"] != DBNull.Value){valores.Add(",['" + dr["NOMEFANTASIA"].ToString() + "'," + dr["QTD"].ToString() + "]");}} valores.Add(", ]");
 comando.Connection.Close();
– Cleber Teixeira
makes a variable
String valor
and within theirifs
, replaces the variablevalores
forvalor
and at the end of hiswhile
addsvalores.Add(valor);
andvalor = String.Empty;
– Enzo Tiezzi
@Enzotiezzi did this and executed with an Alert(Values); to see how he would return. Returned in Alert a System.Collections.Generic.List'1[System.String] but it still continues with the error Unhandled Exception at line 181, column 38 in https://www.google.com/uds/api/visualization/1.0/4e64ac79740513f5765562c361042389/format+pt_BR,default+pt_BR,ui+pt_BR,corechart+pt_BR.I.js 0x800a139e - Javascript runtime error: Not an array when trying to set the variable value date in Ajax
– Cleber Teixeira
gives a look if on the server side, the list is with the desired values.
– Enzo Tiezzi
Let’s go continue this discussão in chat.
– Enzo Tiezzi