Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning an array from remote script

Status
Not open for further replies.

jgales

Programmer
Mar 7, 2003
14
US
I am attempting to return an Array from a remote scripting asp. Write now the code is hard coded with data but my goal will be to load data from a record set and pass the array back to my calling page for display use. My remote page is as follows.

<!--#INCLUDE FILE=&quot;_ScriptLibrary/RS.ASP&quot;-->
<% RSDispatch %>
<script language=&quot;JavaScript&quot; runat=&quot;server&quot;>
var public_description = new MainMethod();
function MainMethod()
{
this.CreateArray = Function('sql','return CreateArray(sql)');

}
</script>

<script language=&quot;JavaScript&quot; runat=&quot;server&quot;>
function CreateArray(sql) {
var OrgArray = new Array();
OrgArray[0]=&quot;1&quot;;
OrgArray[1]=&quot;1.1&quot;;
OrgArray[2]=&quot;1.1.1&quot;;
OrgArray[3]=&quot;1.1.2&quot;;
OrgArray[4]=&quot;1.1.3&quot;;
OrgArray[5]=&quot;1.1.4&quot;;
OrgArray[6]=&quot;1.2&quot;;
OrgArray[7]=&quot;1.5.1&quot;;
OrgArray[8]=&quot;1.5.2&quot;;
OrgArray[9]=&quot;1.5.3&quot;;
OrgArray[10]=&quot;1.3&quot;;
OrgArray[11]=&quot;1.4&quot;;
OrgArray[12]=&quot;1.5&quot;;
OrgArray[13]=&quot;2&quot;;
OrgArray[14]=&quot;2.1&quot;;
OrgArray[15]=&quot;2.1.1&quot;;
OrgArray[16]=&quot;2.1.2&quot;;
OrgArray[17]=&quot;2.1.3&quot;;
OrgArray[18]=&quot;2.2&quot;;
OrgArray[19]=&quot;2.3&quot;;
OrgArray[20]=&quot;2.4&quot;;
return OrgArray;
}
</script>


My calling page has the following code:
<script language=&quot;JavaScript&quot; src=&quot;_ScriptLibrary/RS.HTM&quot;>
</script>

<script language=&quot;JavaScript&quot;>
RSEnableRemoteScripting(&quot;_ScriptLibrary&quot;);
</script>
<script language=&quot;JavaScript&quot;>
var sql=&quot;&quot;;
var objRS = RSGetASPObject(&quot;RSArray.asp&quot;);
var objResult = objRS.CreateArray(sql);
alert(objResult[0]);
</script>

I get undefined from the alert.
 
I've done this before using client side VBScript.

!Here is the worker page
<%@LANGUAGE=&quot;VBSCRIPT&quot;%>
<%Option Explicit
Dim vOption, vSearch, vSQL
If Request.QueryString(&quot;option&quot;) <> &quot;&quot; Then
vOption = Request.QueryString(&quot;option&quot;)
vSearch = Request.QueryString(&quot;search&quot;)
vSQL = &quot;SELECT DonorID, &quot;& vOption &&quot; FROM dbo.DonorInfo WHERE &quot;& vOption &&quot; LIKE '&quot;& vSearch &&quot;%' ORDER BY &quot;& vOption &&quot;;&quot;
Response.Write &quot;<select name='donorid'>&quot;
On Error Resume Next
Set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
objConn.open = strConnect
Set objRS = Server.CreateObject(&quot;ADODB.Recordset&quot;)
objRS.ActiveConnection = objConn
objRS.Source = vSQL
objRS.CursorType = adOpenForwardOnly
objRS.CursorLocation = adUseServer
objRS.LockType = adLockReadOnly
objRS.Open
Call CheckForErrors(objConn)
If NOT objRS.EOF Then
While NOT objRS.EOF
'Create Option List filled with data from DB
Response.Write &quot;<option value='&quot;& objRS.Fields(0).Value &&quot;'>&quot;& objRS.Fields(1).Value &&quot;</option>&quot;& VBCrLf
objRS.MoveNext
WEnd
Else
Response.Write &quot;<option>No Matches Found</option>&quot;
End If
On Error Goto 0
objRS.Close
objConn.Close
Set objRS = Nothing
Set objConn = Nothing
Response.Write &quot;</select><input type='submit' name='Submit' value='Go'>&quot;& VBCrLf
End If
%>

!Here is the page that calls it.
<input type=&quot;radio&quot; name=&quot;option&quot; value=&quot;1&quot;>Option1<br>
<input type=&quot;radio&quot; name=&quot;option&quot; value=&quot;2&quot;>Option2<br>
<input type=&quot;radio&quot; name=&quot;option&quot; value=&quot;3&quot; checked>Option3<br>
<br>
<div id=&quot;results&quot;>Search By:
<select name=&quot;donorid&quot;><option>-----------</option></select>
</div>

<script language=&quot;vbscript&quot;>
function alphabet_onchange
set radioslist = document.all(&quot;option&quot;)
for i=0 to radioslist.length - 1
if radioslist(i).checked then
sOption = radioslist(i).value
end if
next
set radioslist = nothing
set xml = createobject(&quot;microsoft.xmlhttp&quot;)
sAlphabet = document.all(&quot;alphabet&quot;).value
xml.open &quot;post&quot;,&quot;wokerpage.asp?option=&quot;& sOption &&quot;&search=&quot;& sAlphabet &&quot;&quot;, false
xml.send
document.all(&quot;results&quot;).innerhtml = xml.responsetext
set xml = nothing
end function
</script>
 
Thanks for your reply. Having never used XML before I want to make sure I understand what you are saying. The line document.all(&quot;results&quot;).innerhtml = xml.responsetext
will retrieve the data. At this point what do I need to do to present the data to the user? Can I parse through the data? If so how?

Thanks
 
This is client side scripting. What the browser will do is replace what's inside the <div id=&quot;results&quot;> tag with what it gets from the worker page. Any formatting, scripting or whatever you would do on the worker page.
 
From the error and just looking at the code, I'd say the error is from not having the sql variable defined. You should be calling for the return value of the Working Page.I don't use theRSGetObject method, so I can't really speak about it. I do know that your working page need to look like this:

Function Description()

function MainMethod()
{
this.CreateArray = CreateNewArray;
}
public_description = new MainMethod();
</script>
//then name the Function like this:

Function CreateNewArray(sql)
//get your recordset as you have and remember to use
//the Return variable statement

On your calling page I &quot;think&quot; it should look something like this:

<script language=&quot;JavaScript&quot;>
var sql=&quot;&quot;;
var objRS = RSGetASPObject(&quot;RSArray.asp&quot;);
var objResult = objRS.CreateArray(sql);
//alert(objResult[0]);
//This alert should read the value of the return
//object on the working page. &quot;OrgArray&quot;
alert(OrgArray)
</script>

I'm not really that good with javascript either, I use VBScript on my calling page so I can't tell you if the calling page code is correct or not, except the line I changed abo e
Hope this does you a little good


Rob
Just my $.02.
 
I'v got the code to return the array. Thanks for the help. There is one thing I'm still working on. I want to populate the array with data from a recordset. Do I have to retrieve the data from the DB using VBscript or can I use Javascript in my working page?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top