MoOoG
MoOoG

Reputation: 303

How to return an array of values with Ajax using ASP classic

I'm working with ASP classic and Javascript. I have a javascript function where i use a library to create PDFs, but to get values from database I use AJAX. When I make an AJAX call, how would I return an array or hashmap of values, or even each variable separately back to the function from where I make a call???

EDIT: Those values that i want to get from AJAX call, I'd like to use later in the same function to create a pdf.

EDIT:

function make_me_a_pdf(id, izbor, prejemnik){

    var ajaxRequest;  // The variable that makes Ajax possible!
    var spr;

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}

ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){

        spr = ajaxRequest.responseText;
        document.write(spr);

    }

}

ajaxRequest.open("GET", "getPrejetoNarPodatki.asp", true);
ajaxRequest.send(null); 

This is the code I wrote to make an AJAX call. In getPrejetoNarPodatki.asp the only thing that is writen is:

response.write("tralala")

...In this case it saves "tralala" to variable spr and i can use it... My question is how to get more values back so I can use them later in the function make_me_a_pdf() ??

Upvotes: 0

Views: 1919

Answers (3)

MoOoG
MoOoG

Reputation: 303

Ok, for everybody who will search for this problem... I solved it!

I used JSON method to get the values. I put this into the javascript function:

ajaxRequest.onreadystatechange = function(){

    if(ajaxRequest.readyState == 4){

    var txt = ajaxRequest.responseText;
    var obj = eval ("(" + txt + ")");
    alert(obj.employees[1].firstName);      
    }
} 

In the AJAX file i put this lines of code:

text = "{""employees"":[{""firstName"":""John"",""lastName"":""Doe""},                {""firstName"":""Anna"",""lastName"":""Smith""}]}"

response.write(text)

Make sure you use double quotes. The eval() function uses the JavaScript compiler which will parse the JSON text and produce a JavaScript object (taken from w3schools). The printed name in alert is "Anna"

Upvotes: 0

Sam Nunnally
Sam Nunnally

Reputation: 2321

You will probably need to send the PDF response as a URI encoded string after base64 encoding the string and decode it on the client side appropriately. Any examples of how the PDF response is being sent from the server side?

ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){

        var pdfText = decodeURIComponent(ajaxRequest.responseText);

        var pdf = base64DecToArr(pdfText);
        document.write(pdf);
    }

}

/* Array of bytes to base64 string decoding */

function b64ToUint6 (nChr) {

  return nChr > 64 && nChr < 91 ?
      nChr - 65
    : nChr > 96 && nChr < 123 ?
      nChr - 71
    : nChr > 47 && nChr < 58 ?
      nChr + 4
    : nChr === 43 ?
      62
    : nChr === 47 ?
      63
    :
      0;

}

function base64DecToArr (sBase64, nBlocksSize) {

  var
    sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);

  for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
    nMod4 = nInIdx & 3;
    nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
    if (nMod4 === 3 || nInLen - nInIdx === 1) {
      for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
        taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
      }
      nUint24 = 0;

    }
  }

  return taBytes;
}

Upvotes: 0

Tarik
Tarik

Reputation: 11209

Return a JSON array which ends up being plain text that you can generate using classic ASP.

Upvotes: 1

Related Questions