简体   繁体   English

对象不支持此属性或方法 jquery ie8

[英]Object doesn't support this property or method jquery ie8

I have two variables in my script view page, one called products and the other sites.我的脚本视图页面中有两个变量,一个称为产品,另一个称为站点。 I have declared them like that.我已经这样宣布了他们。

Add.cshtml View:添加.cshtml查看:

    $(function () {

         products;
         sites;
        GetProductsAndSites(productsURL, sitesURL, '@Model.Key', false);
     });

I call an ajax function, in another .js file.我在另一个 .js 文件中调用了一个 ajax 函数。

People.js人物.js

   function GetProductsAndSites(productsURL, sitesURL, secondLevelSiteKey, flag) {
$.ajax({
    type: "POST",
    url: productsURL,
    async: false,
    success: function (returndata) {
        if (returndata.ok) {

            **products = returndata.dataNames;**
            //var tempProducts = returndata.dataNames;

            $('#select-product').autocomplete({
                delay: 0,
                minLength: 1,
                source: returndata.dataNames,
                select: function (event, ui) 
                {

                    $.ajax({
                        type: "POST",
                        url: sitesURL,
                        data: { "productID": selectedProductID, "siteID": secondLevelSiteKey },
                        async: false,
                        success: function (returndata) {
                            if (returndata.ok) {

                                  //products = tempProducts ;
                                **sites = returndata.dataNames;**


                                $('#select-site').autocomplete({
                                    delay: 0,
                                    minLength: 1,
                                    source: returndata.dataNames,
                                    select: function (event, ui) {
                                        $("#select-site").val(ui.item.label);


                    });
                }
            });
        }

    }
});

} }

it throws "Object doesn't support this property or method" exception at (products = returndata.dataNames;) line...at first I thought it can not see the products vairalble, but then I realized that it can see the "sites" variable, so I commented the products line and expected that it would throw the same exception at the sites line, but it works just fine.它在 (products = returndata.dataNames;) 行抛出“对象不支持此属性或方法”异常......起初我以为它看不到产品变量,但后来我意识到它可以看到“网站" 变量,所以我评论了产品线,并期望它会在网站线上抛出相同的异常,但它工作得很好。

I have tried creating a local variable and store the returndata.dataNames in it instead of products vairalbe and then set products value before the sites line, but still it throws an exception.我尝试创建一个局部变量并将 returndata.dataNames 存储在其中而不是 products vaairalbe ,然后在站点行之前设置产品值,但它仍然抛出异常。 I tried to put the products line after the sites lines it threw the same exception at the products line also.我试图将产品线放在网站线之后,它也在产品线上抛出了相同的异常。

Help!帮助!

After searching and searching, I finally found my solution.经过搜索和搜索,我终于找到了我的解决方案。 Tested with IE8.用IE8测试。

Override覆盖

if ('XDomainRequest' in window && window.XDomainRequest !== null) {
    jQuery.ajaxSettings.xhr = function() {
        try { 
            return new ActiveXObject("Microsoft.XMLHTTP"); 
        }
        catch(e) { }

        jQuery.support.cors = true;
    }
}

Make the ajax request发出ajax请求

  $.ajax({
      type: 'GET',
      url: "your-url",
      async: true,
      cache: false,
      crossDomain: true,
      contentType: 'application/json; charset=utf-8',
      dataType: 'json'
    })
    .fail(function(jqXHR, textStatus, errorThrown) {
      console.log('ERROR: ' + errorThrown);
    })
    .always(function() {
      // Do stuff
    });

For me this answer helped, where I detect the IE browser and if its lower than 7 or so then set ActiveXObject else XMLHttpRequest.对我来说,这个答案有帮助,在那里我检测到 IE 浏览器,如果它低于 7 左右,则设置 ActiveXObject 否则 XMLHttpRequest。

jQuery.ajaxSettings.xhr: function() {
    // detect if IE and older versions of IE
    if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= 7)
        return new ActiveXObject("Microsoft.XMLHTTP");
    else
        return new XMLHttpRequest();
}

Reference:参考:

Detect IE 7 or lower. 检测 IE 7 或更低版本。

Difference between ActiveXObject and XMLHttpRequest. ActiveXObject 和 XMLHttpRequest 之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM