Get value from AJAX using Javascript and ASP

I am using this Ajax code. But I dont know how i will retrieve my value of value1 on my server-side asp using Javascript.

On my serverside I want to have something like <% var newdata = value1 ( which is the one from the serverside - which was send here) %>

Please Help !!! thanks a million

I know it is possible with PHP but how do i do with javascript

    <script>
function ajaxNow(_data)
{
  var xmlHttp;
  try
  {
    /* Firefox, Opera 8.0+, Safari */
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    /* newer IE */
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      /* older IE */
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser is old and does not have AJAX support!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      /* this puts the value into an alert */
      alert("Value read is: "+xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET","ajax_file.asp?value1="+_data,true);
  xmlHttp.send(null);
}
</script>

Answers:

Answer

When your Ajax-Request succeeds you will have the querystring-variables in the QueryString-Collection of the Request-Object.

Could work like this on the server side:

<% var newdata = Request.QueryString("value1"); %>
Answer

Client-side Javascript can't query server-based databases for obvious reasons. Based on what you appear to be doing, I would suggest you code an ASP which performs the actual query using VBA / C# / whatever, and you can then parse the results in your client-side ajax call as normal.

Answer

URL encode _data and nbquestions variables. Request.QueryString("param1") will decode them for you.

JavaScript URLEncode:

escape(_data);

Also you can use Server.URLEncode() methods from VB script.

Answer

xmlHttp.send correctly writen

  • It doesn't check that you have a 200 status before trying to deal with the data.
  • It fails to encode the data to make sure it is URL safe

I would suggest using a library to handle XHR stuff, instead of reinventing the wheel. Microjs has a list of lots of small libraries if you aren't using one of the large ones (such as YUI or jQuery).

how do I get the values on the server-side using Javascript.

It is just query string data, so it will be in Request.QueryString.

Answer

Whatever the server-side script outputs will be picked up by the AJAX request. So if the AJAX requests something, the server-side does the legwork and fetches the result from the database and then outputs it.

There are loads and loads of tutorials on how to do exactly this. Just ensure that you secure your script properly so that it's not open to abuse.

Answer

you can make the asp page write the result as JSON format read in directly via XMLHttpRequest Object and later processing:

example of JSON

var myJSONObject = {"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};

then you can use native parsers in web browsers or eval() (NOT RECOMENDED, SERIOUSLY!!!) to parse the data written in your asp page and use it in your javascript client code.

More information about JSON basic info

JSON in browsers:

Answer
//(javascript, ajax = xmlHttp)

if your response text is an array you can use this.

var myArray = eval(xmlHttp.responseText);

or if it is a just text you can use .

var value = xmlHttp.responseText

Another approach.This is just a template. If you use jquery, you can use this approach. i hope it solve your problem or give an idea.

html part:

<div id="willupdate"></div>
<div id="willupdate2"></div>

JQuery part:

 $(document).ready(function() {

getValue("serverurl",updateName)
getValue("serverurl",updateSurName)
 });

function updateName(name){
 $("#willupdate").text(name)
}


function updateSurName(name){
 $("#willupdate2").text(name)
}

function updateSurName(name){
 $("#willupdate").text(name)
}

function getValue(url,opt_onRecieved){
    if( !url || url == ""){
        alert("request url error");
        return;
    }

    $.ajax({
        type:"POST",
        url: url,
        dataType:"json",
        success: function(data){
            opt_onRecieved(data);

        }
    });
}
Answer

Here is a very good ajax tutorial. There is everything explained. https://developer.mozilla.org/en/AJAX/Getting_Started

You forget a double quote:

xmlHttp.open("post","CmsAjax.asp",true)

To get the data:

/* this puts the value into an alert */
alert(xmlHttp.responseText);
Answer

You need to encode the data on the server and then decode them in the client. You can use JSON-RPC for this.

Here are few links:

Official Website

Wikipedia Article about JSON-RPC

Implementations of JSON-RPC Service in different languages

But you don't need to use JSON-RPC if you have only one value you can encode as JSON in ASP and then decode it in JavaScript

var array = JSON.parse(xmlHttp.responseText);

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.