简体   繁体   English

从jQuery AJAX调用返回的HTML行为很奇怪

[英]HTML returned from jQuery AJAX call acting weird

I have some HTML that is echoed out by my PHP form handler script. 我的PHP表单处理程序脚本回显了一些HTML。 The call made is an AJAX call, here is the jQuery AJAX call. 进行的调用是AJAX调用,这是jQuery AJAX调用。

$(function() {
    $('#file').bind("change", function() {
        var formData = new FormData();
        //loop for add $_FILES["upload"+i] to formData
        for (var i = 0, len = document.getElementById('file').files.length; i < len; i++) {
            formData.append("file" + i, document.getElementById('file').files[i]);
        }

        //send formData to server-side
        $.ajax({
            url: "file-upload.php",
            type: 'post',
            data: formData,
            dataType: 'html',
            async: true,
            processData: false,  // tell jQuery not to process the data
            contentType: false,   // tell jQuery not to set contentType
            success : function(data) {
                alert('success!!');
                $('#upload-result')[0].html(data);
            },
            error : function(request) {
                alert('error!!');
                console.log(request.responseText);
            }
        });
    });
});

I am getting a weird error: 我收到一个奇怪的错误:

TypeError: undefined is not a function TypeError:未定义不是函数

So I place a breakpoint in my success call, and here is the result in the Chrome Console: 因此,我在成功呼叫中放置了一个断点,这是Chrome控制台中的结果:

控制台调试

As you can see, my div element that I am targeting exists! 如您所见,我定位的div元素存在! my data returned is also present! 我返回的数据也存在! So why can't I append that to the div element #upload-result ? 那么,为什么不能将其附加到div元素#upload-result呢?

When you index a jQuery collection, you get the underlying DOM element. 索引jQuery集合时,您将获得基础DOM元素。 .html() is a jQuery method, not a DOM method. .html()是jQuery方法,不是DOM方法。 It should be: 它应该是:

$('#upload-result').html(data);

or: 要么:

$('#upload-result')[0].innerHTML = data;

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

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