asp.net中添加数据ajax读取不到新数据,请高手指教
--插入数据方法
function insertUser()
{
$.get("inseruser.ashx",{uname:$("#uname").val(),upwd:$("#upwd").val(),uage:$("#uage").val(),usex:$("#usex").val()},function(msg){
$("#aaa").text(msg);
getdata3();
});
}
---得到所有信息方法
function getdata3()
{
$.getJSON(
"UserAllHandler.ashx",
function(data){
var table=$("<table align=center width=60% border=1>"+
"<tr>"+
"<td>编号</td>"+
"<td>姓名</td>"+
"<td>年龄</td>"+
"<td>密码</td>"+
"</tr></table>");
var user=data;
//document.write(user.length);
for(var i=0;i<user.length-1;i++)
{
$("table").empty();
u=user[i];
var row=$("<tr></tr>");
//document.write(key);
var td0=$("<td></td>");
td0.text(i+1);
var td1=$("<td></td>");
td1.text(u.uname);
var td2=$("<td></td>");
td2.text(u.upwd);
var td3=$("<td></td>");
td3.text(u.uage);
td0.appendTo(row);
td1.appendTo(row);
td2.appendTo(row);
td3.appendTo(row);
row.appendTo(table);
}
table.appendTo($("#dd"));
},"json");
}
------------UserAllHandler.ashx文件
<%@ WebHandler Language="C#" Class="UserAllHandler" %>
using System;
using System.Web;
using System.Data;
using System.Text;
public class UserAllHandler : IHttpHandler {
Users user = new Users();
DataSet ds = new DataSet();
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
context.Response.Clear();
this.ds = user.userSelectAll();
StringBuilder sb = new StringBuilder();
//sb.Append("[");
sb.Append("[");
for (int i = 0; i < this.ds.Tables[0].Rows.Count; i++)
{
string uname = this.ds.Tables[0].Rows[i]["uname"].ToString();
string upwd = this.ds.Tables[0].Rows[i]["upwd"].ToString();
string uage = this.ds.Tables[0].Rows[i]["uage"].ToString();
// sb.Append("{\"uname\":\"mike\",\"upwd\":\"123bac\",\"uage\":29},");
sb.Append("{\"uname\":\"" + uname + "\",\"upwd\":\"" + upwd + "\",\"uage\":\"" + uage + "\"},");
}
sb.Append("]");
context.Response.Flush();
context.Response.Write(sb);
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
--插入数据调用insertUser()
后,调用 getdata3()看不到新的数据。
请高手指教。在线等侯。。。。。。谢谢
[解决办法]
context.Response.ContentType = "text/plain";
改成
context.Response.ContentType = "application/json";
或者
var user=jQuery.parseJSON(data);
[解决办法]
AJAX 缓存可能 改成我下面写法
$.getJSON(
"UserAllHandler.ashx?id="+Guid.NewGuid(),
[解决办法]
把get改成post get是只取一次 以后不去取了 post是 都去取新的