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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get output from an asp page and put into a javascript variable

Status
Not open for further replies.

gwu

MIS
Dec 18, 2002
239
US
I am trying to find a way to get the output of an asp page into my javascript. like:

javascript:
Code:
var myOutput = (ouput from test.asp)


test.asp:
Code:
response.write "thisistheoutput"

thanks
 
You need to substitute the values into the javascript code the same way you would insert them into normal html:
Code:
<script language="javascript">
var myOutput = "<%=AspVariable%>";
alert("I got " + myOutput + " from asp");
</script>
Remember that the value will be inserted without quotes or anything, so you need to put the variable substitution inside quotes, as above.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
What I mean is that the asp is on a different page then the javascript

Anyone?
 
Google AJAX


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks! I have tried to implement but have come to a roadblock. I receive a javascript 'permission denied' error.

One thing I did not mention is that the asp page sits on another web application (differnet domain name). I found this quote from one of the tutorials:

Code:
As a security feature, you cannot call pages on 3rd-party domains. Be sure to use the exact domain name on all of your pages or you will get a 'permission denied' error when you call open().

Is there a way around?


Thanks again.
 
Anyone?


Another question...

why can this javascript access 3rd party domains but ajax cannot
Code:
<script language="javascript" src="[URL unfurl="true"]http://www.somedomain.org/test.asp?pagename=<%=pagename%>"></script>[/URL]

Can anyone tell me if I can use this to accomplish what I need:
Code:
<script language="javascript" src="[URL unfurl="true"]http://www.somedomain.org/test.asp?pagename=<%=pagename%>"></script>[/URL]

Thanks
 
AFIAK there shouldn't be a restriction on which site you access via AJAX. The normal reason you will get that error is when you try to access the URL of the page in another window and it is not in the same domain. You certainly don't want a page in one window reporting back on what you're browsing in another window, so this makes sense. However when you are supplying the url yourself it doesn't cause and error (you can open a web page from any domain you want with javascript).

So, anyway, I don't think the error is happening for the reason you think it is. Can you show us some code and tell us where the error is occuring?


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
javascript error(line 29 is http_request.open...):
Code:
Line:29
Char 9
Error:Permission denied
Code:0


code:
Code:
<script type="text/javascript" language="javascript">

    var http_request = false;

    function makeRequest(url) {

        http_request = false;

        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = alertContents;
        http_request.open('GET', url, true);
        http_request.send(null);

    }

    function alertContents() {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                alert(http_request.responseText);
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
</script>
<span
    style="cursor: pointer; text-decoration: underline"
    onclick="makeRequest('[URL unfurl="true"]http://www.someothersite.org/somepage.asp?somevar=somevalue')">[/URL]
        Make a request
</span>
 
>javascript error(line 29 is http_request.open...):
>Error:permission denied

You can obtain the explicit user consent to let pass the request. Expand the open line
[tt]>http_request.open('GET', url, true);[/tt]
to[tt]
if (window.XMLHttpRequest) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
http_request.open('GET', url, true);[/tt]

You can make the setting persists by checking the checkbox to remember the permission given. In that case the setting will be written to user's application data and registry. After then, it won't bother you anymore.
 
Amendment (adding close bracket):
[tt]
if (window.XMLHttpRequest) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
}
http_request.open('GET', url, true);
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top