我有一个问题当我通过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();
}
}
答案 0 :(得分:0)
我已经解决了问题,问题是在javascript方面,我刚忘了添加xhr.setRequestHeader(“Content-Type”, “应用程序/ x WWW的形式进行了urlencoded”);