/    Sign up×
Community /Pin to ProfileBookmark

Need help on getting the results from a URL

Hi everyone!

I’m a complete newbie to JavaScript and I’ve been tasked to do something
that is seemingly very simple, but I can’t quite figure how to do it.

Using JavaScript, I need to call a URL passing it some parameters (this part
I’ve done), now this URL will return a result like the following…

OK|4540c2aa19a4dddf6ca52c2233b87f31

I need to get that result, strip out the ‘OK|’ part, and then pass the
remaining string to another URL (again, I’ve got the passing of parameters
part done). So how can I get this value and store into a variable so that I
can pass it along to another URL?

Thanks!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@CytaelMay 30.2006 — [code=php]
<script type="text/javascript">
firstString = "OK|4540c2aa19a4dddf6ca52c2233b87f31";
anArray = new Array();
anArray = firstString.split('|');
isOK = anArray[0];
keyString = anArray[1];
</script>
[/code]
Copy linkTweet thisAlerts:
@FatbobauthorMay 30.2006 — Thanks for the code Cytael! ?

However, I still need help with getting that value. I don't know how to retrieve the 'OK|4540c2aa19a4dddf6ca52c2233b87f31' from that URL.

For example, when I enter http://www.mcgill.ca/ourserver.html

that returns a page simply displaying...

OK|4540c2aa19a4dddf6ca52c2233b87f31

How do I read that into a variable?
Copy linkTweet thisAlerts:
@CytaelMay 30.2006 — ultimately, you just need to find some way to reference the string in JavaScript and throw it into a variable...the exact method will depend on where/how you're getting the string in the first place. The link posted is just throwing a 404, so I can't help you a whole lot there.
Copy linkTweet thisAlerts:
@phpnoviceMay 30.2006 — For example, when I enter http://www.mcgill.ca/ourserver.html

that returns a page simply displaying...

OK|4540c2aa19a4dddf6ca52c2233b87f31

How do I read that into a variable?[/QUOTE]

If both documents are in the same domain, then you can use an Ajax call to retrieve that response into a string variable for further processing. The following is information on Ajax:

http://jibbering.com/2002/4/httprequest.html
Copy linkTweet thisAlerts:
@FatbobauthorMay 30.2006 — Thanks for the link phpnovice! ?

Using the code from that link, I'm managed to get it to read the string that is returned, but it's not 100% perfect. The problem now is the following, I created a variable called sessionID to store the result, line 4 & 5 displays the result, but line 9 displays 'undefined'.

[CODE]
1 xmlhttp.onreadystatechange=function() {
2 if (xmlhttp.readyState==4) {
3 sessionID = xmlhttp.responseText;
4 alert('Session ID: ' + sessionID);
5 document.write('1) Session ID: ' + sessionID);
6 }
7 }
8
9 document.write('2) Session ID: ' + sessionID);
[/CODE]



This is what's happening (in order), when I run my code...

The following is displayed in the browser...

2) Session ID: undefined

Then a message box appears displaying the correct result.

Then the following is displayed in the browser...

1) Session ID: OK|a06ce2b43f66894ec0f46d7d3596779d

So my problem is that if I try to use the sessionID variable anywhere after line 9 from the above code, that variable is empty. It would appear that my code is not running in order, how can I fix this?

Here is my all of my code...

[CODE]
var xmlhttp = false;
var sessionID;

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}

if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}

xmlhttp.open("GET", "http://198.168.136.155/UserAuthentication?action=createSession&username=sbucciac&authToken=2ff28c18e14373d669e7acf67f27d1ff&pacsUser=sbucciac&clientId=Syscor",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
sessionID = xmlhttp.responseText;
alert('Session ID: ' + sessionID);
document.write('1) Session ID: ' + sessionID);
}
}

document.write('2) Session ID: ' + sessionID);

xmlhttp.send(null)
[/CODE]


BTW Cytael, sorry about that link I provided in my post, it's not accessible from outside our network.
Copy linkTweet thisAlerts:
@phpnoviceMay 30.2006 — This is what's happening (in order), when I run my code...

The following is displayed in the browser...

2) Session ID: undefined

Then a message box appears displaying the correct result.

Then the following is displayed in the browser...

1) Session ID: OK|a06ce2b43f66894ec0f46d7d3596779d
[/QUOTE]

You're getting these results partially because the Ajax call you're using is asynchronous. Thus, you need to change your logic to support that scenario. The other choice is to change the Ajax call to a synchronous call. The last parameter of the Ajax [B]open()[/B] method controls this. [B]true[/B] means asynchronous and [B]false[/B] means synchronous. However, also keep in mind that synchronous calls do not require the use of the [B]onreadystatechange[/B] event.
Copy linkTweet thisAlerts:
@FatbobauthorJun 01.2006 — I got it working as phpnovice suggested by playing around with my logic.

Thanks a lot guys for all you're help, I really appreciate it! ?
Copy linkTweet thisAlerts:
@phpnoviceJun 01.2006 — Excellent!

Cheers.
×

Success!

Help @Fatbob spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...