我正在使用jQuery在代碼中的.ascx控件的方法的AJAX調用後面獲取業務對象的列表:jQuery的阿賈克斯ASP.NET .ASCX控制
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調用執行罰款和收集是斯里alize並返回響應,但我在這裏有一些問題。主要的是沒有創建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無效」任何suggetions?非常感謝你。
如果你有無效的JSON麻煩,無法找到問題,我建議你使用像[JSONLint]一個驗證器(http://jsonlint.com/)。 – 2012-03-22 21:14:13