我正在使用jQuery对后台代码中的.ascx控件的方法进行AJAX调用,以获取业务对象列表:
C#代码:
if (Request.Headers["X-OFFICIAL-REQUEST"] == "TRUE") ReturnList(Request.Params[1]);
protected void ReturnList(string param)
{
Response.Write(GetBusinesses(param));
Response.Flush();
try {
Response.Close();
}
catch { }
Response.End();
return;
}
private string GetBusinesses(string classificationName)
{
System.Web.Script.Serialization.JavaScriptSerializer jSearializer = null;
if (!string.IsNullOrEmpty(classificationName))
{
dSourse = BusinessesDBService.Instance.GetLatestListingsUpdates(classificationName).ToList();
jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();
}
return jSearializer.Serialize(dSourse);
}
jQuery代码:
window.jQuery.ajax({
type: "POST",
async: false,
url: location.href,
dataType: "json",
data: ({ 'FUNCTION': 'ReturnList', 'param0': classificationName.toLowerCase() }),
success: function (data) {
data = eval(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
},
beforeSend: function (xhr) {
xhr.setRequestHeader("X-OFFICIAL-REQUEST", "TRUE"); //Used to ID as a AJAX Request
},
complete: function (XMLHttpRequest, textStatus) {
//build HTML
var tablestring = '<table ><tr><td>UsreID" + "</td><td>UserName</td></tr>';
for (var i = 0, len = data.length; i < len; ++i) {
tablestring = tablestring + "<tr>";
tablestring = tablestring + "<td>" +
data[i].BusinessID + "</td>";
tablestring = tablestring + "<td>" +
data[i].BusinessName + "</td>";
tablestring = tablestring + "</tr>";
}
tablestring = tablestring + "</table>";
var divResult = document.getElementById("divAjax");
divResult.innerHTML = tablestring;
}
});
http调用执行正常,集合被序列化并随响应一起返回,但是我在这里遇到了一些问题。主要的一点是HTML没有被创建 - 我无法捕获返回的字符串。调用完成后,下面的字符串显示在Firebug脚本窗口中
[{“BusinessID”:6549,“BusinessName”:“Ivory Store”,“IsMain”:true},{“BusinessID”:16565,“BusinessName”:“Classic Nails”,“IsMain”},{“ BusinessID“:5877,”BusinessName“:”Visible Changes Hair“,”IsMain“:true}]
同样在Firebug中我注意到XMLHttpRequest响应对象中的字符串和错误消息是“JSON无效”任何建议?非常感谢你。
答案 0 :(得分:1)
无效的JSON
[{"BusinessID":6549,"BusinessName":"Ivory Store","IsMain":true}, {"BusinessID":16565,"BusinessName":"Classic Nails","IsMain"},{"BusinessID":5877,"BusinessName":"Visible Changes Hair","IsMain":true}]
看到中间缺失的部分“IsMain”可能需要
[{"BusinessID":6549,"BusinessName":"Ivory Store","IsMain":true}, {"BusinessID":16565,"BusinessName":"Classic Nails","IsMain":true},{"BusinessID":5877,"BusinessName":"Visible Changes Hair","IsMain":true}]
OR
[{"BusinessID":6549,"BusinessName":"Ivory Store","IsMain":true}, {"BusinessID":16565,"BusinessName":"Classic Nails","IsMain":false},{"BusinessID":5877,"BusinessName":"Visible Changes Hair","IsMain":true}]
请参阅此页面:单击JSLint按钮:http://jsfiddle.net/QmMdC/
答案 1 :(得分:-1)
由于某种原因,下面的代码隐藏C#解析器代码没有将“]”附加到字符串的末尾,所以我不得不手动附加它 - 它工作了!!!谢谢。
public static string Serialize<T>(T obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string retVal = Encoding.UTF8.GetString(ms.ToArray());
return retVal + "]";
}