如何获得 标记来自XMLHttpRequest响应的值并在html中设置

问题描述 投票:-1回答:1

我正在尝试使用XMLHttpRequest上传文件,如果出现错误,我将获得整个错误div作为响应。从那个div我想要段落内的文本,即(文件是空的。请提供逗号分隔的任务。)标签在html页面中设置。在代码中谈论错误代码409。

Request

var xhr = new XMLHttpRequest();       
var url = '/uiris/actions/'+$("#actionName").val()+'/do?time=' + new Date().getTime()+'&subaction=fileupdate';
xhr.open("POST",url, true);
xhr.send(formdata);
xhr.onload = function(e) {

    if (this.status == 200) {
        refreshView();
    } else {
        if (this.status === 409) {
            $('#action').html(this.responseText);  
        } else if (this.status === 401) {
            window.location = "/xyz/";
        } else if (this.status === 422) {
             alert("Unable to perfom task");
             refreshView();
        } else {
            alert("Unable to connect to application");
        }
        refreshView();
    }
}; 

响应:

    <div id="errorDiv" class="margin-top-10-px">
    <h2>Error</h2>
    <p>File is empty.Please provide comma seperated taskids.</p>
</div>
javascript jquery html ajax string
1个回答
1
投票

你可以简单地jQuery-ify和find你想要的text()

$(this.responseText).find("p").text();
© www.soinside.com 2019 - 2024. All rights reserved.