JQUERY json 传值问题
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();
}
}
}
[其他解释]
alert 一下data,看看哪里格式不对
还有,后台可以使用JavaScriptSerializer类进行序列化。前台可以使用jQuery的ParseJSON方法反序列化。
[其他解释]
eval('('+data+')')
试试
[其他解释]
string msg="{ msginfo:[";
msg += "{";
msg += "msgs:'" + "完成"+"',";
msg += "flag:'" + "true" + "',";
msg += "thumb:'" + tpath+"',";
msg += "path:'" + imgpath + "'";
msg += "}"
是不是少了一个]?
[其他解释]
[]是数组,应该是前面多个一个[
[其他解释]
数据格式是对的,我代码没贴全
[其他解释]
[其他解释]
string msg="{ msginfo:[";
msg += "{ msgs:'完成',flag:'true',thumb:'" + tpath + "',path:'"+imgpath+"'}";
msg += "]}";
contentType : "application/json;charset=UTF-8",
dataType : "json",
[其他解释]
有人知道么.
[其他解释]