Solved

Best Way To Return Database Data To Javascript

Posted on 2011-09-28
7
189 Views
Last Modified: 2012-05-12
Hello Experts -
I have some javascript running in an ASP.NET page that requires some data that's resident in a database.  I need the data in any of three forms:  an array, XML or JSON.  What is the least complicated means to accomplish this?  The digging that I've done thus far has me thinking that jQuery's .load() may do the trick, with a target page set to make the query and output the data, either in XML or JSON.  Does this sound right?  If not, what would be better?  If so, can someone please show me how to code a simple ASP.NET (VB.NET) page to render XML or JSON, and then how to code the javascript on the first page to consume it?  I've looked at what seems like a thousand Googled pages and can't seem to find a simple VB.NET-coded example.  Thanks!

-- Rick
0
Comment
Question by:MinnRick
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36768822
I would look at page methods with the AJAX ToolScriptManager, where you could use code-behind to get the data in your JavaScript code...

Proof-of-concept:

Calling a static "page method" from Javascript using MS AJAX
http://www.geekzilla.co.uk/View7B75C93E-C8C9-4576-972B-2C3138DFC671.htm
0
 
LVL 1

Expert Comment

by:danbaugher
ID: 36810155
XML or JSON would work it all depends on what kind of data you need to pull in. Do you need to manipulate the data with JavaScript or is the data already in the correct format? Could you format the data with ASP instead and just output the HTML from the ASP code? You need more information for a better answer.
0
 

Expert Comment

by:andersretteras
ID: 36813229
My best suggestion and the way I normally fetch data from a database to javascript is by using jQuery ajax callbacks to a WCF service (using a serviceProxy) which returns data via JSON.

It might sound a little too much for your scenario at the moment, but doing it this way you will establish a platform of fetching server side data for future projects as well.

Have a look at Rick Strahl's posts here:
http://www.west-wind.com/presentations/jQuery/jQueryPart2.aspx
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MinnRick
ID: 36816122
I do need to manipulate the data into a javascript array if that's the route I end up with.

The use case, by the way, is to grab some SQL Server data to display in JSCharts.  Their documentation on this is limited pretty much to configuring the chart object.  If anyone can assist with some sample code that gathers the data from the client side and then drops it into a JSChart that'd be ideal.  Thanks again!

-- Rick
0
 
LVL 1

Expert Comment

by:danbaugher
ID: 36817745
JSON is probably your best option if you're loading data that needs to be manipulated. Post your problematic code for a better answer.
0
 

Accepted Solution

by:
MinnRick earned 0 total points
ID: 36930753
Thank you again everyone.  I ended up going with an array-based solution for this.  I built a second page that retrieves and response.writes the data into a comma-delimited string.  I then used a jQuery GET from the original page to retrieve and parse (split function) the data from the GET and then iterate through the parsed data to build (push function) the array(s) needed for the JSChart call.  Simple and works great.

-- Rick
0
 

Author Closing Comment

by:MinnRick
ID: 36954251
See above comment for what I did.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question