I am trying to make an ajax json callback using a .net application. So i make a jquery ajax call and i want the data to be sent back in json format. This is what i have done so far.
The requiest is working but the response isnt working.
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);
}
}
});
}
.NET APPLICATION for callback
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
It was the JSON format which i was sending back in wrong format, instead of using ‘ i used “. The double quotes fixed the format and the response now occurs. Kevin B pointed out a JSON Validator, when i tested the JSON with single quotes i got errors but when using double quotes the error were no longer occurring. Thanks