如何将数据发送回JavaScript?例如,我只想使用我的servlet将“Hello World”发送到javascript
的Servlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String environmentName = request.getParameter("startEnvironment");
String pause = Objects.toString(request.getParameter("pause"));
if(pause == "true") {
//I want to send a string "helloworld" back to javascript/ajax
}
JavaScript / AJAX
$(document).on('click','.pauseDocker' ,function(){
var buttonClicked = $(this);
var pauseEnvName = $(this).attr('name');
console.log(pauseEnvName);
//Run Docker
$.ajax({
type: 'post',
url: 'DockerIntegration',
data: {startEnvironment: pauseEnvName, pause: true},
success: function () {
buttonClicked.prop('disabled',true);
},
error: function() {
alert("error");
}
});
});
答案 0 :(得分:1)
response.setContentType("text/plain");
response.getWriter().println("hello world");
这是到处都是最简单的Servlet示例......
编辑:稍加修改以显示消息:
success: function (data) {alert(data);
答案 1 :(得分:0)
//Success function in your ajax request
success: function (data) {
$('#idOfElementWhereYouWantToPrint').text(data);
},
并在HTML页面中打印
REGISTER \path\TrimCols.jar;
DEFINE TrimCols com.company.myproject.TrimCols();
A = LOAD '/path/file.txt' USING TextLoader() AS (line:chararray);
B = FOREACH A GENERATE TrimCols(line);
DUMP B;