什么HTTP错误响应XmlHttpRequest填充responseText字段

What HTTP error response for XmlHttpRequest to fill in responseText field

本文关键字:填充 responseText 字段 XmlHttpRequest 响应 HTTP 错误 什么      更新时间:2023-09-26

我使用XmlHttpRequest向服务器发送请求。请求在服务器上处理,服务器希望发送一个错误用解释性文字回应(在正文中)。我已经尝试了许多不同的响应代码,但在客户端(Chrome v33左右)的XHR对象的响应总是空的。

客户:

   var formItem = document.forms.namedItem("regform"),
       formData = null;
/* -----------------------------------------------------------------
 * General validation - after all the above
 * ----------------------------------------------------------------- */

if (! formItem.checkValidity())
   {
   setup_register();
   return;
   }

formData = new FormData(formItem);
var req = new XMLHttpRequest(),
  url = "http://" + host + "...whatever.../registration";
console.log("Start Registration " + formData);
req.open("POST", url, true);
req.onreadystatechange = function () {
    if (this.readyState == 4)
        {
        if (this.status == 200)
            {
            console.log("Registration finished OK");

                     window.return_window.postMessage("Done", '*');
                    window.close();
                        }

        else
            {
            console.error("Registration failed" + req.status + " Text:" + req.responseText);
            return;
            }
        }
    };
req.send(formData);
服务器:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/registration")
public ClientIdentification registration(
        @FormDataParam("username")  String username,
        @FormDataParam("firstname")  String firstname,
        @FormDataParam("password")  String password,
        @FormDataParam("lastname")  String lastname,
        @FormDataParam("verifypassword")  String verifypassword,
        @FormDataParam("businessname")  String businessname,
  ...
  ...
  throw new PisRsMessageException(e.getMessage(), HttpServletResponse.SC_FORBIDDEN);

其中PisRsMessageException是jersey的WebApplicationException的简单包装

我已经嗅探了这个以确保正确发送响应。然而,要求。responseText总是空的。

我看到这或多或少是W3C标准的简单解释-如果服务器返回错误,responseText应该为空。这没有帮助。我需要有一种返回错误的方式,但仍然有解释性文本。

见:http://www.w3.org/TR/2014/WD-XMLHttpRequest-20140130/the-responsetext-attribute

回退是总是返回200,但如果没有必要,我不想这样做。

看起来我忘记了CORS报头在一个错误的情况下,这就是为什么Chrome是擦除响应文本。