jquery获取后台函数返回值的问题 function FrameDefault() {
var jsonStr = "";
jQuery.ajax({
type:"post",
async:false,
url:"default.aspx/getJsonByFrame?t=" + new Date().valueOf(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的数据用data.d获取内容
jsonStr = window["eval"]("(" + data.d + ")");
},
error: function (err) {
alert(err);//这里报出"<html>
<head>
<title>未知 Web 方法 getJsonByFrame。<br>参数名: methodName</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Lucida Console";font-size: .9em}
.er {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
</style>
</head> <body bgcolor="white"> <span><H1>“/i”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1> <h2> <i>未知 Web 方法 getJsonByFrame。<br>参数名: methodName</i> </h2></span> <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "> <b> 说明: </b>执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 <br><br> <b> 异常详细信息: </b>System.ArgumentException: 未知 Web 方法 getJsonByFrame。<br>参数名: methodName<br><br> <b>源错误:</b> <br><br>
}
});
search_content_array_Frame = jsonStr;
load_html_start_default_frame(1);
} [WebMethod]
public string getJsonByFrame()
{
List<Frame> list = GetAllFram();
string jsonStr;
jsonStr = "[";
for (int i = 0; i < list.Count; i++)
{
jsonStr += "{\"imgSrc:\"" + list[i].FrameImagePath + "\",\"framePrice\":\"" + list[i].Price + "\",\"frame\":\"" + list[i].FrameImageName + "\"},";
}
jsonStr = jsonStr.Substring(0, jsonStr.LastIndexOf(','));
jsonStr += "]";
jsonStr = jsonStr.Replace(@"\", "$");
Console.Write(jsonStr);
return jsonStr;
}
var jsonStr = "";
jQuery.ajax({
type:"post",
async:false,
url:"default.aspx/getJsonByFrame?t=" + new Date().valueOf(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的数据用data.d获取内容
jsonStr = window["eval"]("(" + data.d + ")");
},
error: function (err) {
alert(err);//这里报出"<html>
<head>
<title>未知 Web 方法 getJsonByFrame。<br>参数名: methodName</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Lucida Console";font-size: .9em}
.er {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
</style>
</head> <body bgcolor="white"> <span><H1>“/i”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1> <h2> <i>未知 Web 方法 getJsonByFrame。<br>参数名: methodName</i> </h2></span> <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "> <b> 说明: </b>执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 <br><br> <b> 异常详细信息: </b>System.ArgumentException: 未知 Web 方法 getJsonByFrame。<br>参数名: methodName<br><br> <b>源错误:</b> <br><br>
}
});
search_content_array_Frame = jsonStr;
load_html_start_default_frame(1);
} [WebMethod]
public string getJsonByFrame()
{
List<Frame> list = GetAllFram();
string jsonStr;
jsonStr = "[";
for (int i = 0; i < list.Count; i++)
{
jsonStr += "{\"imgSrc:\"" + list[i].FrameImagePath + "\",\"framePrice\":\"" + list[i].Price + "\",\"frame\":\"" + list[i].FrameImageName + "\"},";
}
jsonStr = jsonStr.Substring(0, jsonStr.LastIndexOf(','));
jsonStr += "]";
jsonStr = jsonStr.Replace(@"\", "$");
Console.Write(jsonStr);
return jsonStr;
}
function FrameDefault() {
var jsonStr = "";
jQuery.ajax({
type:"post",
async:false,
url:"default.aspx/getJsonByFrame?t=" + new Date().valueOf(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的数据用data.d获取内容
jsonStr = window["eval"]("(" + data.d + ")");
},
error: function (err) {
alert(err);
}
});
search_content_array_Frame = jsonStr;
load_html_start_default_frame(1);
}本来想把错误信息粘贴出来的 没想到出来那么多东西 意思就是后台 不存在这个方法
[WebMethod] public static string getJsonByFrame() { List<Frame> list = GetAllFram(); string jsonStr; jsonStr = "["; for (int i = 0; i < list.Count; i++) { jsonStr += "{\"imgSrc:\"" + list[i].FrameImagePath + "\",\"framePrice\":\"" + list[i].Price + "\",\"frame\":\"" + list[i].FrameImageName + "\"},"; } jsonStr = jsonStr.Substring(0, jsonStr.LastIndexOf(',')); jsonStr += "]"; jsonStr = jsonStr.Replace(@"\", "$"); Console.Write(jsonStr); return jsonStr; }
lz既然都用jquery了,还是改为正宗ajax,用ashx文件返回json数据吧
public string getJsonByFrame()
{
List<Frame> list = GetAllFram();
string jsonStr;
jsonStr = "[";
for (int i = 0; i < list.Count; i++)
{
jsonStr += "{\"imgSrc:\"" + list[i].FrameImagePath + "\",\"framePrice\":\"" + list[i].Price + "\",\"frame\":\"" + list[i].FrameImageName + "\"},";
}
jsonStr = jsonStr.Substring(0, jsonStr.LastIndexOf(','));
jsonStr += "]";
jsonStr = jsonStr.Replace(@"\", "$");
Response.Write(jsonStr);
return jsonStr;
}
function FrameDefault() {
var jsonStr = "";
jQuery.ajax({
type:"post",
async:false,
url:"default.aspx?t=" + new Date().valueOf(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//返回的数据用data.d获取内容
jsonStr = window["eval"]("(" + data.d + ")");
},
error: function (err) {
alert(err);}
});
}C#( default.aspx ) :
public void getJsonByFrame()
{
List<Frame> list = GetAllFram();
string jsonStr;
jsonStr = "[";
for (int i = 0; i < list.Count; i++)
{
jsonStr += "{\"imgSrc:\"" + list[i].FrameImagePath + "\",\"framePrice\":\"" + list[i].Price + "\",\"frame\":\"" + list[i].FrameImageName + "\"},";
}
jsonStr = jsonStr.Substring(0, jsonStr.LastIndexOf(','));
jsonStr += "]";
jsonStr = jsonStr.Replace(@"\", "$");
Response.Write(jsonStr);
Respnse.End();
}
在页面加载事件中调用:getJsonByFrame() 方法,前提是default.aspx 在也没有其他操作哈,如果有你最好独立出来
success: function (data) {
//返回的数据用data.d获取内容
jsonStr = window["eval"]("(" + data.d + ")");
},在这个方法里面的时候 现在是可以data.d是有值的 但是我单步运行的时候 直接就运行到ajax方法的外面了 然后在提示的时候 jsonstr的值就又是空了
var jsonStr = "";
jQuery.ajax({
type: "post",
url: "Default.aspx/getJsonByFrame",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
//返回的数据用data.d获取内容
jsonStr = window["eval"]("(" + data.d + ")");
alert(data.d);
},
error: function (err) {
alert(err);
}
});
success里面的alert不执行
然后alert(data)不是data.d
调试了半个小时 感觉 错误就是出现在这里的 不知道该怎么转换 因为在后台代码的返回值是string 所以在这里的data也是字符串 带引号的 要是直接写成 jsonstr=data的话 jsonstr的格式就是string了 不满足要求 该如何能够实现eval的方法的呢 能够让jsonstr的值为json类型
改为:
JSON2.parse(data.d);