简体   繁体   中英

How to get <p> tag value from XMLHttpRequest response and set in html

I am trying to upload file using XMLHttpRequest and in case of error, I am getting the whole error div in response. From that div , I want the text inside paragraph ie(File is empty.Please provide comma seperated taskids.) tag to set in HTML page. Talking about error code 409 in code.

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();
    }
}; 

Response:

    <div id="errorDiv" class="margin-top-10-px">
    <h2>Error</h2>
    <p>File is empty.Please provide comma seperated taskids.</p>
</div>

你可以简单地 jQuery 化它并find你想要的text()

$(this.responseText).find("p").text();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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