1.js调用ajax
$("#submit").click(function(){ </span>
<span style="white-space:pre"> </span>$.ajax({
<span style="white-space:pre"> </span>url:'/ycf/push_news',
<span style="white-space:pre"> </span>data:{
<span style="white-space:pre"> </span>"title":title, //参数形式:参数名:参数值
<span style="white-space:pre"> </span>"from":from,
<span style="white-space:pre"> </span>"stockCode":stockCode,
<span style="white-space:pre"> </span>"content":content,
<span style="white-space:pre"> </span>"push":push
<span style="white-space:pre"> </span>},
<span style="white-space:pre"> </span>type:'post',
<span style="white-space:pre"> </span>dataType:'json', //接收数据形式:我设置成了json。
<span style="white-space:pre"> </span>success:function(data) {
<span style="white-space:pre"> </span>alert(data.msg);
<span style="white-space:pre"> </span>},
<span style="white-space:pre"> </span>error:function() {
<span style="white-space:pre"> </span>alert("访问后台异常");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
)}
<span style="white-space:pre"> </span>JSONObject jsonObject = new JSONObject();
jsonObject.put("msg", "插入成功"); //返回了一个键为msg的,值是 “插入成功”的信息。 ajax用 data.msg获取信息
PrintWriter out = response.getWriter();
out.print(jsonObject);
out.flush();
out.close();