嘗試和工作。您正在使用,
<textarea name='Status'> </textarea>
<input type='button' onclick='UpdateStatus()' value='Status Update'>
我使用JavaScript,(不知道PHP), 使用ID = 「狀態」 中的textarea像
<textarea name='Status' id="status"> </textarea>
<input type='button' onclick='UpdateStatus()' value='Status Update'>
然後撥打電話與Servlet發送狀態到後端在你的UI在腳本標籤
<srcipt>
function UpdateStatus(){
//make an ajax call and get status value using the same 'id'
var var1= document.getElementById("status").value;
$.ajax({
type:"GET",//or POST
url:'http://localhost:7080/ajaxforjson/Testajax',
// (or whatever your url is)
data:{data1:var1},
//can send multipledata like {data1:var1,data2:var2,data3:var3
//can use dataType:'text/html' or 'json' if response type expected
success:function(responsedata){
// process on data
alert("got response as "+"'"+responsedata+"'");
}
})
}
</script>
和JSP使用任何strutucre(如MVC的java或任何其它的)你喜歡的更新,這樣就像是
the servlet will look like: //webservlet("/zcvdzv") is just for url annotation
@WebServlet("/Testajax")
public class Testajax extends HttpServlet {
private static final long serialVersionUID = 1L;
public Testajax() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String data1=request.getParameter("data1");
//do processing on datas pass in other java class to add to DB
// i am adding or concatenate
String data="i Got : "+"'"+data1+"' ";
System.out.println(" data1 : "+data1+"\n data "+data);
response.getWriter().write(data);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
第一'.'應該是'+'在'URL: '阿賈克斯/ StatusUpdate.php狀態=?' 狀態,數據: 「」,數據類型:「json'',因爲它的JavaScript不是PHP。 – ncm
大家請把-1的原因做一些有用的事情。 – ncm
@imsiso其次呢? –