最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AJAX and IE - Unable to get property error - Stack Overflow

programmeradmin2浏览0评论

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);
                }

            }
        });

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);
                }

            }
        });
Share Improve this question edited May 29, 2018 at 17:56 Michael asked May 25, 2018 at 16:03 MichaelMichael 4431 gold badge11 silver badges32 bronze badges 6
  • Did you see this: stackoverflow./questions/32279630/… – justDan Commented May 25, 2018 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 Commented May 25, 2018 at 16:57
  • I'm not too sure to be honest. I'd try to use type checks like he mentioned in the ments if (typeof myVar !== 'undefined') { ... }. I'll give you a bump and hopefully someone else will chime in. – justDan Commented May 25, 2018 at 16:59
  • Thank you. I appreciate it! Hoping someone else chimes in...I'm a bit stuck! – Michael Commented May 25, 2018 at 17:09
  • Why do you have to set dataType to text for IE? – Musa Commented May 25, 2018 at 17:46
 |  Show 1 more ment

3 Answers 3

Reset to default 5 +50

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);
                }

            }
        });

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.

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.

发布评论

评论列表(0)

  1. 暂无评论