在Ajax调用期间,有时会出现TypeError“undefined”

时间:2014-08-07 14:57:51

标签: javascript jquery html ajax

这里是我的ajax请求和响应,我有大约85个HTML页面具有相同的ajax请求。当我使用这些文件时有时会出现以下错误

enter image description here

AJAX

$(document).ready(function(){
    localStorage.setItem('currentPageNo', 9);
    ResetSwiper();
    CheckPageReadCompleted();
    extrapopuptrigger("1");
});


function ResetSwiper() {
    toggle_text = localStorage.getItem("currentBibleVersion");
    myView = $(".scrollpane").data("mobileIscrollview");
    if(toggle_text == "ESV") {
        $(".searchContent").hide();
        $(".esvContent").show();
        setTimeout(function() {
        var text_search = null;
        $(".esvContent").html('Loading...');
        text_search = $(".searchTitle").html();
        xhr = $.ajax({                      
            type: "GET",
            url: "http://www.esvapi.org/v2/rest/verse?key=IP&passage="+text_search+"&include-footnotes=false",
            data:"",
            contentType: "text/html",
            dataType: "text",
            cache: false,
            async: true,
            crossDomain: true,
            success: function(resp) {
                $(".esvContent").html(resp);
                setTimeout(function() {
                    if(myView != null || myView != 'undefined') {
                        myView.refresh();
                    }
                },100);
            },
            error: function(err) {
                var is_uiwebview = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent);
            if(is_uiwebview) {
                  natviveAlert("StandFIRM","Unable to connect internet.Please try again!");
            } else {
                window.JSInterface.showAlertDialog("StandFIRM","Unable to connect internet.Please try again!");
            }
            }
        },100);
    });
    } else{
        $(".esvContent").hide();
        $(".searchContent").show();
        myView.refresh();
    }
 }

我如何解决这个问题?任何人都可以帮我解决

1 个答案:

答案 0 :(得分:2)

undefined不应该有引号

   if(myView != undefined) {
     myView.refresh();
    }

修改

由于@filoxo建议您可以使用undefined的引号,但在比较之前应添加typeof

   if(typeof myView != 'undefined') {
     myView.refresh();
    }

选中此link