Spring HttpRequestHandler + XMLHttpRequest



我有一个问题,当我通过JavaScript发送发布数据时,HttprequestHandler不会收到任何数据。我想收到内容的价值,但它不起作用。这是JavaScript代码:

function utils_saveElementAndGetId(url,content) {
var xhr = new XMLHttpRequest()
xhr.open("post", url, false);
xhr.send(content);
if (xhr.status != 200) {
    alert(xhr.status + ': ' + xhr.statusText)
} else {
    return xhr.responseText
}
}

这是httprequesthandler的代码:

  public class HeaderServlet implements HttpRequestHandler {
private static final Logger log = LoggerFactory.getLogger(HeaderServlet.class);
TemplateDao templateDao;
HeaderElementDao headerElementDao;
CheckboxElementDao checkboxElementDao;
@Autowired
public HeaderServlet(TemplateDao templateDao, HeaderElementDao headerElementDao, CheckboxElementDao checkboxElementDao) {
    this.templateDao = templateDao;
    this.headerElementDao = headerElementDao;
    this.checkboxElementDao = checkboxElementDao;
}
public void handleRequest(HttpServletRequest req,
                          HttpServletResponse resp)
        throws ServletException, IOException {
    String content = req.getParameter("content");
    HeaderElement headerElement = new HeaderElement(content);
    Long templateId = (Long) req.getSession().getAttribute("id");
    Template template = templateDao.get(templateId);
    headerElement.template = template;
    headerElementDao.create(headerElement);
    template.headerElements.add(headerElement);
    templateDao.saveOrUpdate(template);

    resp.setStatus(200);
    resp.setContentType("text/plain");
    resp.getOutputStream().println(headerElement.getId());
    resp.flushBuffer();
}
}

我解决了问题,问题是在javascript方面,我只是忘了添加xhr.setrequestheader(" content-type", "应用程序/X-WWW-Form-urlenCoded");

相关内容

  • 没有找到相关文章

最新更新