Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从Servlet保存字符串_Javascript_Html_Ajax_Servlets - Fatal编程技术网

Javascript 从Servlet保存字符串

Javascript 从Servlet保存字符串,javascript,html,ajax,servlets,Javascript,Html,Ajax,Servlets,我有一个HTTPServlet,它将数据发送到我的数据库。我还想在中显示该数据(它是一个字符串) 为此,我尝试使用AJAX var responseText = ''; $.ajax({ type: "POST", url: "/InsertServlet", success : function(text) { responseText = text; } }); alert(res

我有一个HTTPServlet,它将数据发送到我的数据库。我还想在
中显示该数据(它是一个字符串)

为此,我尝试使用AJAX

var responseText = '';
$.ajax({ type: "POST",   
         url: "/InsertServlet",   
         success : function(text)
         {
             responseText = text;
         }
});

alert(responseText);
不幸的是,字符串是空的。 有人有主意吗?
Mfg.

示例代码创建空字符串responseText,然后使用jQuery ajax方法设置ajax调用(但尚未调用),最后报告responseText,此时它仍然是一个空字符串。直到ajax调用和“success”函数触发后,才会填充responseText变量

将alert()移动到ajax“success”函数中

var responseText = '';
$.ajax({ type: "POST",
         url: "/InsertServlet",   
         success : function(text)
         {
             responseText = text;
             alert(responseText);
         } });

这对我很有用,不幸的是,我在Servlet中调用response.sendRedirect(…),我得到的纯html代码是新字符串O.O。仍然谢谢:)