23

It works fine in chrome, firefox and IE8. But comes up an error on IE7. Here is my jquery onchange event.

$('select#NationId').change(function () {
        var nationId = $(this).val();
        $.ajax({
            url: 'LoadAreas',
            type: 'POST',
            data: JSON.stringify({ nationId: nationId }),
            dataType: 'json',
            contentType: 'application/json',
            success: function (data) {
                $('select#AreaId').get(0).options.length = 0;
                $('select#AreaId').append('<option value="0">Select All</option>');
                $.each(data, function (val, Areas) {
                    $('select#AreaId').append('<option value="' + Areas.Id + '">' + Areas.Name + '</option>');
                });
            }
        });
    });

controller

[HttpPost]
    public ActionResult LoadAreas(int nationId)
    {
        var _Areas = (from c in SessionHandler.CurrentContext.ChannelGroups
                      join cgt in SessionHandler.CurrentContext.ChannelGroupTypes on c.ChannelGroupTypeId equals cgt.ChannelGroupTypeId
                      where cgt.Name == "Area" && c.ParentChannelGroupId == nationId
                      select new AreaName() { Id = c.ChannelGroupId, Name = c.Name }).OrderBy(m => m.Name);

        if (_Areas == null)
            return Json(null);
        List<AreaName> managers = (List<AreaName>)_Areas.ToList();

        return Json(managers);
    }
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
bladerunner
  • 889
  • 4
  • 16
  • 25

4 Answers4

29

The issue is that the JSON object is not available in IE 7. You'll want to include JSON2.js on your page for IE < 8 users.

Community
  • 1
  • 1
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • i tried . The onchange event does not get fired. – bladerunner May 24 '11 at 19:02
  • 1
    First, I'm assuming you are *not* actually hotlinking to JSON2 ;-) Second, is the *event* not firing at all? (And is this only in IE7?) – Sean Vieira May 24 '11 at 21:07
  • I copied the script that was in json2.js file, added that to my project and referenced that file on my master page. Thats all i did. – bladerunner May 24 '11 at 21:32
  • @bladerunner - if you `alert(nationId);` in your `.change` handler, nothing is alerted? Or do you get errors? (And if so, what errors?) – Sean Vieira May 25 '11 at 00:24
  • Sorry i was gone on PTO. This was fixed. Copied json2.js file and included that in my master page. Tested in IE7 and the onchange event started working. – bladerunner May 26 '11 at 21:48
  • @SeanVieira i am getting the same problem that's why i have added the json2.js file in html but now t's giving me the erro "Object doesn't support property or method 'exec'" in my "jquery-1.11.1.min.js" file any suggestions on this? – cracker Oct 13 '14 at 12:58
1

If the browser doesn't implement the JSON object, you can always use a third-party library to provide it for you. If I recall correctly, this particular implementation is widely used and defers to the browser, so you just need to drop it in, no tweaking required.

Mauricio
  • 1,683
  • 12
  • 18
  • Hi Kyte, So i copied json2.js in my project and referenced that file in my script header. The dropdown does not work at all. – bladerunner May 24 '11 at 19:01
  • 2
    @bladerunner: That's because [IE7 doesn't handle change() properly](http://stackoverflow.com/questions/1637503/jquery-change-event-on-select-not-firing-in-ie). – Mauricio May 24 '11 at 20:10
0
$('select#NationId').change(function () {
    var nationId = $(this).val();
    var data = '{"nationId": "' + nationId + '"}';

    $.ajax({
        url: 'LoadAreas',
        type: 'POST',
        data: data ,
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            $('select#AreaId').get(0).options.length = 0;
            $('select#AreaId').append('<option value="0">Select All</option>');
            $.each(data, function (val, Areas) {
                $('select#AreaId').append('<option value="' + Areas.Id + '">' + Areas.Name + '</option>');
            });
        }
    });
});
jLuna
  • 321
  • 2
  • 3
0

Shouldn't

data: { "nationId": nationId },

just work?

ZippyV
  • 12,540
  • 3
  • 37
  • 52