0

I'm trying to implement a JSON call to simulate AJAX on a certain page where an AJAX panel isn't a viable option.

I want call my .aspx page when a State is selected from a drop down and populate the Counties drop down.

in my State dropdown, I have this call:

onchange="jsonDropDownLoader('COUNTIES', this, 'Content2_DDLCounties')"

That call is on the page and the code is here:

function jsonDropDownLoader(sType, oParent, oChild) {
  var lstrChild = document.getElementById(oChild);
  var lstrFilter = ""

  if (oParent.value > "") {
    lstrFilter = oParent.value
  }

  lstrChild.options.length = 0;
  if (oParent.value > "") {
    var JSONobject = {};
    var http_request = new XMLHttpRequest();
    url = "/AltairWeb.NET/RS/jsonDropDownLoader.aspx?TYPE=" + sType + "&FILTER=" + lstrFilter
    http_request.open("GET", url, false);
    http_request.onreadystatechange = function () {
        var done = 4, ok = 200;
        if (http_request.readyState == done && http_request.status == ok) {
            JSONobject = JSON.parse(http_request.responseText);
        }
    };
    http_request.send(null);
    var JSONarray = eval('(' + http_request.responseText + ')').data
    for (var i = 0; i < JSONarray.length; ++i) {
        var optn = document.createElement("OPTION");
        optn.text = JSONarray[i].text;
        optn.value = JSONarray[i].value;
        lstrChild.options.add(optn);
    }
  }
}

It returns a string which I then use to populate the County drop down.

I'm getting data back, but it's not rendering on your QA server. Using the developer tools with IE8, I can see that I have a error on this line:

JSONobject = JSON.parse(http_request.responseText);

it says that JSON is not declared.

It says I also have a syntax error on this line:

var JSONarray = eval('(' + http_request.responseText + ')').data

This works perfectly on my development box. However, my development box has WinXP / IIS 5 on it, whereas, our QA server is a Win2008 server with IIS7.5. We have new development boxes coming, but until then, I'm stuck with the XP machine.

Since it works locally, it seems like it must be a security issue with either Windows or IIS on the QA server, possibly with the http_request call, but I can't find anything via google that has helped me figure this out.

I know I've seen posts that JSON.parse is not supported by IE prior to IE9, but this works perfectly in IE8 when I point to my dev server, but not when I point to the QA server, so it doesn't seem to be a browser issue. Any ideas?

user229044
  • 232,980
  • 40
  • 330
  • 338
tkflick
  • 13
  • 1
  • 3
  • 1
    Is compatibility mode turned on? `JSON` wasn't defined in IE7. By the way, this has nothing to do with Windows 2008, but with IE8 instead and maybe with its options. – MaxArt Jul 11 '12 at 22:25
  • Please [don't add signatures or taglines to your posts](http://stackoverflow.com/faq#signatures). – user229044 Jul 12 '12 at 16:09

2 Answers2

1

JSON.parse() is a function of your browser, not the server.

Are you sure the difference is the server ... and not your client browser???

You might also wish to consider using something like jQuery (which can both simplify your coding, and help mediate cross-browser issues like this). for example:

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • It's not the client browser. I'm on my laptop, and have each environment open in it's own tab. So same browser, it works in one environment and not in the other. I get the same results if I use Firefox btw.. it works when pointed to my dev server, but not when pointed to the QA server. That's what leads me to think it's not an IE issue. – tkflick Jul 11 '12 at 22:43
  • 1) It would definitely be useful if you could post the *exact* error messages. 2) A "JSON not defined" error strongly implies the browser ... but that might just be a red herring. I don't know. 3) In any case, life would be much happier if it's at all possible for you to use jQuery (or Dojo) ... IMHO... – paulsm4 Jul 12 '12 at 00:31
  • heh... you're correct sir, it was a red herring! See my post above. I'm new to json, and I didn't even write this code. The developer who wrote it, is in Hawaii for a couple of weeks, so I ended up being the one how had to fix this once we deployed it and it failed in QA. – tkflick Jul 12 '12 at 16:05
1

First, you are using a synchronous call (xhr.open('GET', url, false)) , and you are also using onreadystatechange . This is wrong. Choose one or the other.

https://developer.mozilla.org/en/xmlhttprequest

enter image description here

Next, check your browser support for JSON. See https://stackoverflow.com/a/891306/48082 . If you are unsure, then use json2.js from json.org.

Finally, do not use eval. Use a proper JSON library.

Community
  • 1
  • 1
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • As I said, I know my browser supports it, because it worked perfectly when pointed to the dev environment. I did not know about the syncronous call / onreadystatechange issue, thank you, I'll correct that. – tkflick Jul 12 '12 at 15:49
  • I did finally figure out what the problem was. It was basically an id10t error. The IIS directory structure is different locally than on the actual web servers. Locally, our url is http://localhost/AltairWeb.Net/ But on the web servers it would be http://qa.altair.org/ So the actual AltairWeb.Net folder is replaced by qa.altair.org. That's why when I hit the codebehind page directly, it returned records, but not through the javascript/json call... the json call was actually throwing a page not found error on the http_request.responseText statement. Thanks for all the help. – tkflick Jul 12 '12 at 15:49