Ajax Uncaught TypeError:无法重新定义属性:

时间:2018-06-08 18:26:19

标签: jquery ajax

请帮我摆脱这个问题, 当我运行以下ajax请求代码以填充选择框时,具体取决于用户对第一个选择框的选择:

$("#item_type").change(function () {
var item_type = $("#item_type").val();

if(item_type == "spare_parts")
{
    $.ajax({
        type: "GET",
        url: "http://example.com/index.php/inventory/spend_receive_receipt/get_items/",
        dataType: 'json',
        success: function (data) {
            var Areas = data.length;
            elements = "<option value='0'>إختر الصنف</option>";
            for (var i = 0; i < Areas; i++) {
                elements += "<option value='" + data[i]['id'] + "'>" + data[i]['item_name'] + "</option>";
            };
           elements_label = "الأصناف";
            $("#items_devices").html(elements);
    //                  $("#items_devices_label").html(elements_label);
        };
        error: function (req, err) {
            console.log("my message : " + err);
        };
    });
}else if (item_type == "devices")
{
    $.ajax({
        type: "GET",
        url: "http://example.com/index.php/inventory/spend_receive_receipt/get_devices/"
        dataType: "json",
        success: function (data) {
            var Areas = data.length;
            elements = "<option value='0'>إختر الجهاز</option>";
            for (var i = 0; i < Areas; i++) {
                elements += "<option value='" + data[i]['id'] + "'>" + data[i]["device_name"] + "</option>";
            }
           elements_label = "الأجهزة";
            $("#items_devices").html(elements);
//                      $("#items_devices_label").html(elements_label);
        }
        error: function (req, err) {
            console.log("my message : " + err);
        };
    });

}
});

我在consol中遇到以下错误,请求未成功

Uncaught TypeError: Cannot redefine property: BetterJsPop
    at Function.defineProperty (<anonymous>)
    at inject (<anonymous>:20:10)
    at <anonymous>:510:11
    at <anonymous>:511:11

和这个

Uncaught TypeError: Cannot set property 'innerHTML' of null
    at new countUp (countUp.min.js:1)
    at autoUpdateNumber (dashboard.js:2)
    at HTMLDocument.<anonymous> (dashboard.js:1)
    at j (jquery-1.11.min.js:776)
    at Object.fireWith [as resolveWith] (jquery-1.11.min.js:810)

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这适合我,但我不知道问题是什么

function get_items_devices(item_type) {
if(item_type == 'spare_parts')
{
    item_type = 'spare_parts';
}else if(item_type == 'devices'){
    item_type = 'devices';
}

if (item_type == 'spare_parts'){
$.ajax({
    type       : "GET",
    url        : 'http://example.com/index.php/inventory/spend_receive_receipt/get_items/',
    dataType   : 'json',
    success: function(data) {

          var Areas = data.length;
          elements = "<option value='0' disabled selected>إختر الصنف</option>";
            for (var i = 0; i < Areas; i++) {
                elements += "<option value='" + data[i]['id'] + "'>" + data[i]['item_name'] + "</option>";
            }
          $("#items_devices").html(elements);
        var elements_label = "الأصناف";
        $("#items_devices_label").html(elements_label);
    },
    error: function(req, err){ console.log('my message : ' + err); }
});
}else if (item_type == 'devices'){
$.ajax({
    type       : "GET",
    url        : 'http://example.com/index.php/inventory/spend_receive_receipt/get_devices/',
    dataType   : 'json',
    success: function(data) {

          var Areas = data.length;
          elements = "<option value='0' disabled selected>إختر الجهاز</option>";
            for (var i = 0; i < Areas; i++) {
                elements += "<option value='" + data[i]['id'] + "'>" + data[i]['device_name'] + "</option>";
            }
          $("#items_devices").html(elements);
        var elements_label = "الأجهزة";
        $("#items_devices_label").html(elements_label);
    },
    error: function(req, err){ console.log('my message : ' + err); }
});
}

}