11

I'm getting the following error Unable to get property ‘replace’ of undefined or null reference on line var ajax_html = $(xml).find("#search-ajax-content").html(); when using AJAX on IE (testing in IE11). This code is functioning fine on other browsers (Chrome, FF, and Safari).

Has anyone ever experienced this issue before using AJAX? I'm not exactly sure how to resolve this issue. Any help is appreciated! Thank you!!

    $.ajax({
            type:"GET",
            dataType:"xml",
            url:"/search-ajax/" + window.location.search + "&pagination=" + page,
            success: function(data) {
                var xml = data;

                if (page == 1)
                {
                    $("#wait-element-container").remove();


                    // Issue is happening here only on IE!
                    var ajax_html = $(xml).find("#search-ajax-content").html();
                    $("#postload-target").append(ajax_html);
                }

            }
        });
Michael
  • 403
  • 1
  • 9
  • 28
  • Did you see this: https://stackoverflow.com/questions/32279630/jquery-ie9-10-unable-to-get-property-replace-of-undefined-or-null-reference – justDan May 25 '18 at 16:30
  • Hmm I just read through that. I'm not how I should wait for this element to load though? Any ideas? – Michael May 25 '18 at 16:57
  • I'm not too sure to be honest. I'd try to use type checks like he mentioned in the comments `if (typeof myVar !== 'undefined') { ... }`. I'll give you a bump and hopefully someone else will chime in. – justDan May 25 '18 at 16:59
  • Thank you. I appreciate it! Hoping someone else chimes in...I'm a bit stuck! – Michael May 25 '18 at 17:09
  • Why do you have to set dataType to text for IE? – Musa May 25 '18 at 17:46
  • [This](https://stackoverflow.com/questions/44147648/error-thrown-with-microsoft-xmldom-xml-parser-in-edge-with-xml-string-content)? – wazz May 25 '18 at 18:27

3 Answers3

5

jQuery is able to parse text and query as HTML (as long as the text is valid html). Have you tried:

$.ajax({
            type:"GET",
            dataType:"text",
            url:"/search-ajax/" + window.location.search + "&pagination=" + page,
            success: function(data) {
                var xml = data;

                if (page == 1)
                {
                    $("#wait-element-container").remove();


                    // Issue is happening here only on IE!
                    var ajax_html = $(xml).find("#search-ajax-content").html();
                    $("#postload-target").append(ajax_html);
                }

            }
        });
malkassem
  • 1,937
  • 1
  • 20
  • 39
  • WOW! This worked!!! Any ideas why the change from xml to text would fix this issue?! – Michael May 29 '18 at 18:14
  • @Michael doesn't make sense...you had: dataType:$.browser.msie ? "text" : "xml", this is the same as dataType: "text", and it shouldn't work...unless your browser is not IE or it fails to detect it somehow...anyway, glad it works for you now. – Foxy May 29 '18 at 18:24
  • Yea that's what I was thinking too. I'm surprised it's working now. I'll do some more testing to make sure it's working 100%....keep you posted – Michael May 29 '18 at 19:03
0

I remember having this issue (but) when working on AJAX with ASP.NET. I don't know if this may help but I'll post it anyway. Notice the "d" in the XML's response parse. This is located at a Javascript file:

function ajaxCall() {
    $.ajax({
        /* Code ommited for brevity */
        ...
        success: 'myFunction_OnSuccess',
        ...
    });
}

function myFunction_OnSuccess(response) {
    /* On NetFramework 2.0 and older'*/
    /*var xmlDoc = $.parseXML(response);*/

    /*For NetFramework 3.5+'*/
    var xmlDoc = $.parseXML(response.d);
    var xml = $(xmlDoc);

    var msg = xml.find("msg").text();
}

This is the way I handle my Ajax's onSuccess responses.

Máster
  • 981
  • 11
  • 23
  • Thanks for your post Santiago! Quick question...when outputting console.log(xmlDoc) I'm getting null. I'm wondering if I'm doing something wrong with my xml structure? Here's what is outputted when console.log(xml) http://prntscr.com/jo67uu – Michael May 29 '18 at 17:28
  • I could be the `response` not being returned in the proper format, XML in this case. I'm on VB/C# so I return it this way, I work with plain text though, notice the "return ds.GetXml()": `[WebMethod] public static string WebMethodExample() { DataSet ds = new DataSet(); DataTable dt; dt.Columns.Add("msg"); dt.Rows.Add(); dt.Rows(0)("msg") = "This is my AJAX response message"; return ds.GetXml(); }` You'll have to copy-paste to your editor for better viewing. – Máster May 29 '18 at 17:47
  • Hmmm...i actually don't think that is it. Could there be a timeout in the XML loading on IE? – Michael May 29 '18 at 17:57
0

It looks like you have issue only with IE because the data type you set:

dataType:$.browser.msie ? "text" : "xml",

change it to

dataType: "xml",

and it should be ok.

Foxy
  • 416
  • 8
  • 18