jquery Ajax, query a DB

295 views
Skip to first unread message

Marco Megna

unread,
Nov 7, 2012, 6:22:39 AM11/7/12
to google-visua...@googlegroups.com
Hi,
I use this script for acquire data from my sql DB with classic asp :
data.asp
<!--#include file="JSON_2.0.4.asp"-->
<!--#include file="JSON_UTIL_0.1.1.asp"-->
<%
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open Application("Conn")
        dim strsql
        strsql="SELECT field1 FROM table_name"
QueryToJSONobj(objConn, strsql).Flush
%>
 
and this to show the data:
chart.asp
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart','table']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "Data.asp",
dataType:"json",
async: false,
success: function(){alert('OK');},
error: function(responseText){alert('err: ' + responseText);}
}).responseText;
var data = new google.visualization.DataTable(jsonData);
var table = new google.visualization.Table(document.getElementById('chart_div2'));
table.draw(data, {allowHtml: true, showRowNumber: true});
}
</script>

everything works fine, but i want to improve it.
i have to do multiple queries, soI want to send the query to data.asp from chart.asp
The idea is that data.asp should become a dynamic page for retrieve any query  sent to it; something like:

Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open Application("Conn")
        dim strsql
        strsql=request("strsqlquery")
QueryToJSONobj(objConn, strsql).Flush

one solution could be pass it from the url and use request.querystring:
 
var jsonData = $.ajax({
url: "Data.asp" + "?strsql=" + encodeURIComponent("SELECT field1 FROM table_name"),
dataType:"json",
async: false,
success: function(){alert('OK');},
error: function(responseText){alert('err: ' + responseText);}
}).responseText;

it works, but there's a chars limit and with long complex queries doesn't work...
maybe with ajax it's possible to do something better...
any suggestion?

Thanks!
Marco


asgallant

unread,
Nov 7, 2012, 11:07:03 AM11/7/12
to google-visua...@googlegroups.com
Set the "type" parameter to "POST" and put your query in the "data" parameter.  See the jQuery AJAX documentation for details.

Marco Megna

unread,
Nov 7, 2012, 11:42:14 AM11/7/12
to google-visua...@googlegroups.com
Quite simple...Thanks a lot!

Marco
Reply all
Reply to author
Forward
0 new messages