Solved

REST service in Classic ASP ?

Posted on 2014-07-21
11
2,089 Views
Last Modified: 2014-07-21
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
Comment
Question by:JElster
  • 4
  • 4
  • 3
11 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 40209406
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
 
LVL 1

Author Comment

by:JElster
ID: 40209417
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
 
LVL 33

Expert Comment

by:Big Monty
ID: 40209425
are you trying to build a REST service or consume one?
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!

 
LVL 33

Accepted Solution

by:
Big Monty earned 400 total points
ID: 40209467
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
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 100 total points
ID: 40209587
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
 
LVL 1

Author Comment

by:JElster
ID: 40209593
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40209636
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
 
LVL 1

Author Comment

by:JElster
ID: 40209934
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40209979
sql = "SELECT * FROM table WHERE some_field = 'foo'"

or

foo = request.querystring("bar")
sql = "SELECT * FROM table WHERE some_field = '"&foo&"'"
0
 
LVL 1

Author Comment

by:JElster
ID: 40209984
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
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40210015
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with query 3 36
Display if field is NOT "" or NOT null 6 22
Building JSON Results Table FROM DB 9 32
Ajax on ASP 2 34
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

740 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