Can javascript act like a web service and return a parameter value received in a query string to the client that posted the query? I am trying to return a query parameter in C# with no success. For example, if the query string is http://www.mypage/service?hubchallenge=1234 what javascript code would be used to return the value 1234 to the client without returning the web page itself?
Asked
Active
Viewed 82 times
0
-
1you need to look at AJAX for how to do it.. I'm not a C# guy so don't know about implementation – Arun P Johny Aug 06 '13 at 02:55
-
See the solutions in [here][1] and [here][2] [1]: http://stackoverflow.com/questions/831030/how-to-get-get-request-parameters-in-javascript [2]: http://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript – Dilantha Aug 06 '13 at 02:58
-
I believe the suggestions will return a value from a function. However, I want to return the value to a client that posted the query. Can that be done in javascript? – Dave Aug 06 '13 at 03:55
-
By returning a value using these suggestions, the whole html page is returned after pageload. I don't want to return the page, just the value. Can that be done? – Dave Aug 06 '13 at 03:56
-
Basically I want to do an echo like in PHP only do it in javascript – Dave Aug 06 '13 at 04:02
2 Answers
1
You should have to you AJAX for it in your page. It cannot be done without passing a request from the client. The below javascript code has to be placed in the page which send request.
function test()//the function can be called on events
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for other browsers
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET"," http://www.mypage/service?hubchallenge=1234",true);
xmlhttp.send();
}
0
In javascript, you can get the url into a string like this:
var urlString=document.URL;
then you can parse out parameters like
var qs=urlString.split("?")[1];
var qsArray=qs.split("&");

chiliNUT
- 18,989
- 14
- 66
- 106