Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2682
  • Last Modified:

REST service in Classic ASP ?

How can I create a rest service in old classic asp. Or create asp page that will serve or provide data that can be used
with JQUERY  to read the data.   Use an ASP page as the DAL and have have HTML page and JQuery read from the asp
page and populate a grid.
0
JElster
Asked:
JElster
  • 4
  • 4
  • 3
2 Solutions
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
have a go at this:

<% 
if Request("mode") = "1" then    '-- ajax call to REST service
dim objXMLHTTP, results
set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.Open "GET", "http://www.website.com", false 
objXMLHTTP.SetRequestHeader "Content-type", "text/html" 
objXMLHTTP.Send

results = objXMLHTTP.ResponseText
%> 

Open in new window


with this bit of code, you now have all of the data in a variable called results.

all you have to do is make an ajax call to that bit of code:

$(document).ready(function(){

    $('#btnGetData').bind('click', function (event) {
		 $.ajax({
			 type: "POST",
			 url: "page.asp",
			 data: "mode=1,
			 cache: false,
			 success: function(html) {	
                               //-- parse your data here and put into the grid. if you expect the data back in JSON format, you can use the jquery parseJSON() function
                         }
		 });
    });
});

Open in new window


what kind of grid are you using and what format do you expect the data returned as?
0
 
JElsterAuthor Commented:
I need to query a DB.. I guess using
Set connection = Server.CreateObject("ADODB.Connection")

and get a dataset.

Then how do I have jquery read / parse the dataset?

Using Kendo UI grid or someother Jquery grid.. not sure.

thx
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
are you trying to build a REST service or consume one?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
if you're trying to pull data out of database to put into a grid, i would skip the "REST service" part of it for now, and just get the basics down. first thing I would do is familiarize yourself with aspJSON, a library that'll allow you easily build and manipulate json data in classic asp. you can find the component here:

https://code.google.com/p/aspjson/

second, choose which jquery you want to use. I prefer jqGrid, mainly because it's simple and easy to set up.

once you have downloaded the files and put them in your site, it's easy to code the html:

            <div id="gridContainer" align="center">
                <table id="users"></table>
            </div>

Open in new window


that's it! of course, you need to reference all of the files that are needed by the grid as well.

to initialize the grid and load it with data, you can use the following as a guide to get you going in the right direction:

$(document).ready(function () {
    $("#users").jqGrid({
        datatype: "json",
        url: "/webServices/userSearch.asp",
        gridview: true,
        emptyrecords: "no users to view",
        colNames: ['column1', 'column2', 'column3', 'column4'],
    });

Open in new window


what this code does is make a call to a page and if it receives data in a json format, it'll display it in the grid.

your userSearch.asp page (in this example) will look like this:

<!--#include virtual = "/includes/plugins/aspJSON/JSON_2.0.4.asp"-->
<!--#include virtual = "/includes/plugins/aspJSON/JSON_UTIL_0.1.1.asp"-->
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connectionString

sql = "select * from table"
QueryToJSON( conn, sql ).Flush

if conn.State <> 0 then conn.Close
set conn = nothing
%>

Open in new window

this code, using the aspJSON component, will query the database and display the results in a json format. The QueryToJSON() function is actually writing out the data to the screen, and since it's an ajax call, it spits out the data back to the ajax call, which then translates it to the grid
0
 
Scott Fell, EE MVEDeveloperCommented:
no points

fyi, a small portion of kendo is not open source and the grid is one of those items and only available if you pay.  For a grid, you can use www.datatables.net.  There is a built in ajax function  http://www.datatables.net/examples/data_sources/ajax.html

Looking at that example, you just need basic html like this
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
 
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>

Open in new window


The asptojson code that monty is leading you to will use the json objects sample http://www.datatables.net/examples/ajax/objects.html rather than the default array sample.  The key is to check your output in a json linter to make sure it is valid http://jsonlint.com/
0
 
JElsterAuthor Commented:
Thanks.. it is possible to include all the code in one ASP file?  Or do I need a separate 'userSearch.asp' and another HTML or ASP page?
0
 
Scott Fell, EE MVEDeveloperCommented:
You will need 2 pages and it looks like Big Monty is leading you to making your ajax page http:Q_28480375.html#a40209467

Then you will need  a main page as well.
0
 
JElsterAuthor Commented:
Any idea how I can add more than one query on a page..

<!--#include virtual = "/includes/plugins/aspJSON/JSON_2.0.4.asp"-->
<!--#include virtual = "/includes/plugins/aspJSON/JSON_UTIL_0.1.1.asp"-->
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connectionString

sql = "select * from table"
QueryToJSON( conn, sql ).Flush

if conn.State <> 0 then conn.Close
set conn = nothing
%>
0
 
Scott Fell, EE MVEDeveloperCommented:
sql = "SELECT * FROM table WHERE some_field = 'foo'"

or

foo = request.querystring("bar")
sql = "SELECT * FROM table WHERE some_field = '"&foo&"'"
0
 
JElsterAuthor Commented:
How can I have more than of these on  page...

sql = "select * from Customers"
 QueryToJSON( conn, sql ).Flush


sql = "select * from Orders"
 QueryToJSON( conn, sql ).Flush
0
 
Scott Fell, EE MVEDeveloperCommented:
If request.querystring("type")="customers" then

             sql = "select * from Customers"
             else
             sql = "select * from Orders"
end if

QueryToJSON( conn, sql ).Flush

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now