Hello Sitepoint,
I am trying to implement some AJAX(using jQuery) in my company’s CMS system and am running into a couple problems. I have read all over online and looked at quite a few examples but just can’t seem to get this to work or understand the best practice when communicating with methods on the server through AJAX.
I would like to just pass a JSON object into .NET method, deserialize into an object, insert into db, and serialize the object back to the caller without using WCF, etc. The problem is I keep receiving this error message: No parameterless constructor defined for type of \u0027System.String\u0027. If anyone could give me some insight or comprehensive examples to read through I would very much like you forever:)
My JSON as I build it from the form:
var data = '{"replacementPartsDiagram":{"diagramTitle": "' + DiagramTitle + '","diagramImagePath": "' + DiagramImagePath + '","diagramSortOrder": ' + DiagramSortOrder + ',"flattenedDiagramRefNumbers": "' + FlattenedDiagramRefNumbers + '"}}';
The actual post request going through(according to firebug):
{"replacementPartsDiagram":{"diagramTitle": "Foo Bar","diagramImagePath": "Koala.jpg","diagramSortOrder": 3,"flattenedDiagramRefNumbers": "1,44394,2,45194,--,47092,3,45202,4,44403,5,45200,6,44405,7,44407,8,44409,9,44411"}}
Calling jQuery Ajax method(being called through a jQuery UI dialog add button):
$.ajax({
type: 'POST',
url: 'categories.aspx/addRPDiagram',
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
context: this,
timeout: 5000,
success: function (msg) {
alert(msg.d);
/*if (msg.d) {
alert("Successfully entered the new diagram");
}
else {
alert("Error! The diagram was not entered into the database!");
}*/
$(this).dialog("close");
},
error: function (xhr, status, errorThrown) {
alert("Error!" + xhr.responseText + ' ' + errorThrown);
}
});
The webmethod in .NET. I just wanted to see if I could get anything returned at this point:
<WebMethod()> _
Public Shared Function addRPDiagram(replacementPartsDiagram As String) As String
Dim serializer = New JavaScriptSerializer()
Dim rep As ReplacementPartsDiagram = serializer.Deserialize(Of ReplacementPartsDiagram)(replacementPartsDiagram)
Return rep.DiagramTitle
End Function
The ReplacementPartsDiagram Class:
Public Class ReplacementPartsDiagram
Private _diagramTitle As String
Private _diagramImagePath As String
Private _diagramSortOrder As Integer
Private _flattenedDiagramRefNumbers As String
Public Sub ReplacementPartsDiagram()
End Sub
Public Property DiagramTitle As String
Get
Return _diagramTitle
End Get
Set(value As String)
If _diagramTitle = value Then
Return
End If
_diagramTitle = value
End Set
End Property
Public Property DiagramImagePath As String
Get
Return _diagramImagePath
End Get
Set(value As String)
If _diagramImagePath = value Then
Return
End If
_diagramImagePath = value
End Set
End Property
Public Property DiagramSortOrder As Integer
Get
Return _diagramSortOrder
End Get
Set(value As Integer)
If _diagramSortOrder = value Then
Return
End If
_diagramSortOrder = value
End Set
End Property
Public Property FlattenedDiagramRefNumbers As String
Get
Return _flattenedDiagramRefNumbers
End Get
Set(value As String)
If _flattenedDiagramRefNumbers = value Then
Return
End If
_flattenedDiagramRefNumbers = value
End Set
End Property
End Class
I just can’t seem to wrap my head around the best way to do this as there is so many examples everywhere. Any direction would be awesome.
Thanks guys!