62,243
社区成员




string msg="{ msginfo:[";
msg += "{";
msg += "msgs:'" + "完成"+"',";
msg += "flag:'" + "true" + "',";
msg += "thumb:'" + tpath+"',";
msg += "path:'" + imgpath + "'";
msg += "}";
$.ajax({
type: "POST",
dateType: "json",
url: "xxxx.aspx",
data: "path=" + path,
success: function (data) {
var jsonData;
jsonData = eval(data);
$.each(jsonData.msginfo, function (idx, item) {
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var path="path"
$.ajax({
type: "POST",
dateType: "json",
url: "Default8.aspx",
data: "path=" + path,
success: function (jsonData) {
jsonData = $.parseJSON(jsonData);
$.each(jsonData.msginfo, function (idx, item) {
alert(this.msgs);
});
}
});
$.getJSON("Default8.aspx","path=" + path,function (jsonData) {
$.each(jsonData.msginfo, function (idx, item) {
alert(this.msgs);
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
页面后置文件:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["path"] == "path")
{
string tpath = "http://avatar.profile.csdn.net/B/0/6/1_yw39019724.jpg";
string imgpath = "http://avatar.profile.csdn.net/B/0/6/1_yw39019724.jpg";
string msg = "{ \"msginfo\":[";
msg += "{";
msg += "\"msgs\":\"" + "完成" + "\",";
msg += "\"flag\":\"" + "true" + "\",";
msg += "\"thumb\":\"" + tpath + "\",";
msg += "\"path\":\"" + imgpath + "\"";
msg += "}";
msg += "]}";
Response.Write(msg);
Response.End();
}
}
}
按你的思路,修正了一些你的小错误。
不过, 我前面说的, 你还是得牢记, json格式是严格的, 手工效率低, 而且很易出错。 最好是用工具转。 .net 自带的JavaScriptSerializer或者Newtonsoft.dll 来转吧。
此外,最好是建立 ashx 文件来返回数据。 这样效率比较高。
string msg="{ msginfo:[";
msg += "{ msgs:'完成',flag:'true',thumb:'" + tpath + "',path:'"+imgpath+"'}";
msg += "]}";