2012-01-20 62 views
1

我想使用.net應用程序進行ajax json回調。所以我做了一個jQuery的ajax調用,我希望數據以json格式發回。這是我迄今爲止所做的。.Net Ajax與JQuery和JSON不回撥

最重要的是工作,但答覆isnt工作。

JQUERY AJAX


function Ajax_Callback(param,callback) { 

$.ajax({ 
    url: "default.aspx?param=" + param, 
    dataType: "json", 
    contentType: "application/json; charset=utf-8", 
    success: function(data) { 
     if (callback) { 
      callback.call(null, data); 
     } 
    } 
}); 

} 

回調


Private Sub BaseControls_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

     If Me.Page.Request.Headers.Item("X-Requested-With") = "XMLHttpRequest" Then 

      Me.Page.Response.Clear() 
      Me.Page.Response.ContentType = "application/json" 
      Me.Page.Response.Write(Me._GetHTMLServerResponseCallback) 
      Me.Page.Response.Flush() 
      Me.Page.Response.End() 

     End If 


    End Sub 

Private Sub Main_RaiseCallbackReference(ByVal pageType As BaseControls.PageEnumType, ByVal data() As String) Handles Me.RaiseCallbackReference 

     Select Case pageType 

      Case PageEnumType.Main 


       Dim request As String = data(0) 

       If request = "ThreadCreated" Then 

        Dim idThread As Integer = CInt(data(1)) 

        Dim sb As New StringBuilder() 
        Dim sw As New StringWriter(sb) 
        Dim htw As New HtmlTextWriter(sw) 

        Dim userThreadControl As New UI.UserThreadControl(idThread) 
        Me.Controls.Add(userThreadControl) 

        userThreadControl.RenderControl(htw) 

        Dim html As String = sb.ToString 

        Me._GetHTMLServerResponseCallback = "[ { title: 'One', key: '1' }, { title: 'Two', key: '2' } ]" 

       End If 


     End Select 

    End Sub 
+0

你是什麼意思,當你說「迴應沒有工作?」你有沒有得到任何答覆?檢查Firebug或Chrome以查看您收到的響應。 –

+0

@MattGrande,回調發生的中斷點客戶端並沒有被解僱,所以我推測回調不起作用,但生病檢查螢火蟲,謝謝 – redoc01

+0

將AJAX網址粘貼到瀏覽器中,看看你得到了什麼。 –

回答

1

.NET應用程序這是我在錯誤的格式發回的JSON格式,而不是使用「我使用「。雙引號固定格式和響應現在發生。凱文B指出了一個JSON驗證器,當我測試JSON與單引號我有錯誤,但使用雙引號時,錯誤不再發生。謝謝

Private Sub Main_RaiseCallbackReference(ByVal pageType As BaseControls.PageEnumType, ByVal data() As String) Handles Me.RaiseCallbackReference 

     Select Case pageType 

      Case PageEnumType.Main 


       Dim request As String = data(0) 

       If request = "ThreadCreated" Then 

        Dim idThread As Integer = CInt(data(1)) 

        Dim sb As New StringBuilder() 
        Dim sw As New StringWriter(sb) 
        Dim htw As New HtmlTextWriter(sw) 

        Dim userThreadControl As New UI.UserThreadControl(idThread) 
        Me.Controls.Add(userThreadControl) 

        userThreadControl.RenderControl(htw) 

        Dim html As String = sb.ToString 

        Me._GetHTMLServerResponseCallback = "{ ""data"": ""<div></div>"",""threadID"": 6 }" 

       End If 


     End Select 

    End Sub