简体   繁体   中英

How can I get attribute of ajax response

resp.getWriter().write("msg=1?id=" + l1); 

in the below code I can get responseText but how can I get the attribute of response text.

ajax code:

function updatecategories(cu) {
  var r1 = new XMLHttpRequest();
  r1.onreadystatechange = function() {
    if (r1.readyState == 4 && r1.status == 200) {
      if (r1.responseText = "1") {
        // how to get id from the response.
      }
    }
  };
  r1.open("GET", "../category_update?action=catu&cu=" + cu, true);
  r1.send();
}

You can set small information into the response header as name-value pairs. That way it's easier to retrieve.

Set the response header

response.setHeader("msg", "1");
response.setHeader("id", l1); //value must be a String.

Retrieve the information

if (r1.readyState == 4 && r1.status == 200) {
    var msg = r1.getResponseHeader("msg");
    var id = r1.getResponseHeader("id");
}

Side note: when setting information into the response header make sure you always use a unique name . Avoid using names like

  • status
  • date
  • content-length
  • content-type

since they are already reserved.

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