Dave
Dave

Reputation: 923

How can javascript return a query parameter to client

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?

Upvotes: 0

Views: 90

Answers (2)

user2036918
user2036918

Reputation:

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();
} 

Upvotes: 1

chiliNUT
chiliNUT

Reputation: 19573

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("&");

Upvotes: 0

Related Questions