我正在尝试使用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>
你可以简单地jQuery-ify和find
你想要的text()
:
$(this.responseText).find("p").text();