0

I have AJAX (I think) returning data from a server call.

function showCustomer(str) {
    var xmlhttp;
    if (str=="") {
        document.getElementById("txtHint").innerHTML="";
        return;
    }

    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "getcustomer3.asp?q=" + str, true);
    xmlhttp.send();
}

The call returns this:

<script>var CC2=101;var CC4=105;</script>

The 101 & 105 are data from my DB.

I have a function in the page that the data is returned to. This is the function:

function unhideCC() {
    $("#" + CC2).show();
    $("#" + CC4).show();
}

I am calling the function from a button onClick event, and I wait to click said button until after the data has been returned, which I thought would work.

I am struggling to get the variables written back to the page to load into the function. I have a strong feeling I'm going about this in the entirely wrong way. Any pointers to get me on the correct path?

Thanks ahead of time!

EDIT: This is the code that returns the values:

response.write("<script>")
dim counter: counter = 0
do until rs.EOF
  for each x in rs.Fields
    counter = counter + 1
    'Use Mod() to check counter is even
    If counter Mod 2 = 0 Then response.write "var CC" & counter & "=" & x.value & ";"
  next
  rs.MoveNext
loop
response.write("")
response.write("</script>")
7
  • 2
    You think you have ajax?? Post your code! Commented May 16, 2014 at 14:40
  • @ElliotM updated, sorry! Was trying to not bloat the question but I guess that is important info haha Commented May 16, 2014 at 14:42
  • 1
    Your AJAX code is not calling unhideCC() at any point...? Commented May 16, 2014 at 14:44
  • 1
    It's important information because it shows us that you're doing this the way hard way! If you're using jquery, check out jquery.get Commented May 16, 2014 at 14:44
  • 1
    The reason I (and the below answer) suggest using jquery to make your ajax is because jquery parses your return value into native data "for free", otherwise you'll have to do the work to extract the values from your call return. Commented May 16, 2014 at 14:57

1 Answer 1

2

First, can't your web server return json structure instead of :

<script>var CC2=101;var CC4=105;</script>

As json, it would look like this:

{
   "CC2" : 101,
   "CC4" : 105
}

It will then be much easier to access those variables. Or else you will need to parse your original data structure to extract the information or use eval(..) which is much more ugly.

With JQUERY your ajax call would look something like this:

$.ajax({
    type: "GET",
    url: "someurl",
    datatype: "json",
    data: ?,
    success: function(jsonServerData){

        var CC2 = jsonServerData.CC2;
        var CC4 = jsonServerData.CC4;

    },
    error: function(status, xht, err){
    }
});

refer to Jquery ajax documentation

Sign up to request clarification or add additional context in comments.

5 Comments

I added what the server call is in my question. Would it work if I just changed the response.write setups to return it in the format you listed?
Is your server using ASP.NET? Almost every languages have built-in functions to convert your objects into a JSON string.
It won't work, because your ajax call doesn't automatically do anything with the return value to extract the data you want. If you re-write the ajax call using jquery as suggested here, it'll do exactly what you want.
@TchiYuan yes the server is using ASP.NET
@Brett try this link to figure out how to return json from ASP.NET: johnnycode.com/2012/07/16/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.