[英]Unable to send json data to Java servlet using AJAX
I am trying to pass json data of a datatable and the email the user inputs to my java servlet.我正在尝试将数据表的 json 数据和用户输入的 email 传递给我的 java servlet。 I get a status 200 but no data response.我得到一个状态 200 但没有数据响应。 I have my AJAX code here:我在这里有我的 AJAX 代码:
$.ajax({
url: "/bin/abc/sendrohssearchresult",
type: "POST",
contentType: "application/json",
data:{
"email": email,
"emailSubject": emailSubject
},
success: function(status){
console.log("success",status);
},
error:function(error){
console.log("error",error);
},
})
And my Java servlet:还有我的 Java servlet:
@Component(service= Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Send Email of RoHS Search Results",
"sling.servlet.methods=" + HttpConstants.METHOD_POST,
"sling.servlet.paths="+ "/bin/abc/sendrohssearchresult"
})
public class RohsEmailServlet extends SlingAllMethodsServlet {
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String emailSubject = request.getParameter("emailSubject");
}
}
I have tried other solutions I've found here on stackoverflow such as JSONify the data, remove dataType: json , and adding contentType: application/x-www-form-urlencoded;我已经尝试过我在 stackoverflow 上找到的其他解决方案,例如 JSONify 数据,删除dataType: json ,并添加contentType: application/x-www-form-urlencoded; charset=UTF-8;字符集=UTF-8; but still the same result.但仍然是相同的结果。 I am using AEM.我正在使用 AEM。
As @rakhi4110 mentioned, I needed to add response.getWriter()
in order to receive a data response.正如@rakhi4110 提到的,我需要添加response.getWriter()
以接收数据响应。 So I added:所以我补充说:
response.getWriter().write("Success Data");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.