Solved

REST service in Classic ASP ?

Posted on 2014-07-21
11
1,725 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 32

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 32

Expert Comment

by:Big Monty
ID: 40209425
are you trying to build a REST service or consume one?
0
 
LVL 32

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now