2012-03-22 69 views
0

我正在使用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?非常感謝你。

+0

如果你有無效的JSON麻煩,無法找到問題,我建議你使用像[JSONLint]一個驗證器(http://jsonlint.com/)。 – 2012-03-22 21:14:13

回答

-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 + "]"; 
    } 
+1

嘗試「沖洗」 MemoryStream。如果它實際上並沒有附加尾部']',這將是框架中的一個重大缺陷,並且其他幾個人也會遇到類似的問題。 – 2012-03-26 18:33:41

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/

+0

我的不好,IsMain上的「缺失部分」沒問題,當截斷實際的JSON時,我失去了「真正的」部分 - 約30個字段。我明天會發布整個對象的JSON。謝謝。 – itisinteresting 2012-03-22 22:41:51

+0

出於某種原因,下面的代碼隱藏C#解析器代碼沒有將「]」追加到字符串的末尾,所以我不得不手動追加它 - 它的工作!謝謝。公共靜態字符串序列化(T OBJ) { DataContractJsonSerializer串行=新DataContractJsonSerializer(obj.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms,obj); string retVal = Encoding.UTF8.GetString(ms.ToArray()); return retVal +「]」; } – itisinteresting 2012-03-26 18:22:15

+0

是的,我看到你的C#代碼,它是definatly不是我做的方式,在上課,讓框架處理序列的該類似乎是做這個的方式.. – 2012-03-27 11:42:57