XMLHttpRequest 사용시 주의사항

2021. 1. 24. 18:29Javascript

XMLHttpRequest의 변수를 동일하게 써서 2개를 처리하면 에러 발생

변수명을 변경하면 해결됨

xhr, xhr -> xhr, xhr1

            function init() {
                if(canvas == null) {
                    var xhr = new XMLHttpRequest();
                    xhr.responseType = 'arraybuffer';
                    xhr.open('GET', "./kor.exp0.tiff");
                    xhr.onload = function(e) {
                        var tiff = new Tiff({buffer: xhr.response});
                        canvas = tiff.toCanvas();
                        canvas.setAttribute('id','tiff')
                        context = canvas.getContext('2d')
                        console.log('원본(w,h): ',canvas.width,canvas.height)
                        document.body.append(canvas);
                    }
                    xhr.send();
                } else {
                    document.body.append(canvas);
                }

                if(boxes == null) {
                    var xhr1 = new XMLHttpRequest();
                    xhr1.responseType = 'text';
                    xhr1.open('GET', "./kor.exp0.box");
                    xhr1.onload = function(e) {
                        boxes = xhr1.responseText.split('\n');
                        $.each(boxes, function(i,box) {
                            boxes[i] = box.split(' ')
                        })
                    }
                    xhr1.send();
                }
            }