Question

Hope someone can help. Really pulling my hair out and starting to think i should have not used wcf data services. Its been easy to get odata from the service so i thought i could send json object from my javascript code and read the contents as an object in my service But it returns nothing.

My javascript:

 var vname = [];
 var obj = { myobject: { frmid: "test", frmval: "1111" } }
 vname.push(obj)

 $.ajax({
      url: "MyWCFDataService.svc/SendItems",
      type: "POST",
      dataType: "json",
      contentType: "json",
      data: { myobject: JSON.stringify(vname) },
      success: function () {
          alert("success :-)");
      },
      error: function () {
          alert("fail :-(");
      }
  });

My class and function in my svc

<DataServiceKeyAttribute("id")> _
Public Class tobject
    Public Property id As Integer
    Public Property frmid As String
    Public Property frmval As String
End Class

<WebInvoke()> _
Public Function SendItems(myobject As String) As Boolean
           ' have to ask for string as errors when asking for tobject
    Return True ' nothing here yet as cannot get json object
End Function

My first venture into wcf data services and jquery. Was hoping to return a list of textboxes names and values to a wcf data service to process. Is it possible with the wcf data service?

Was it helpful?

Solution

Ok so i created a new wcf data service, removed the inherits dataservice reference and initializeservice sub.

javascript:

vname.push( { frmid: "test", frmval: "1111" })

$.ajax({
  url: "MyWCFDataService.svc/SendItems",
  type: "POST",
  dataType: "json",
  contentType: "application/json; charset=utf-8",
  data: { JSON.stringify(vname) },
  success: function () {
      alert("success :-)");
  },
  error: function () {
      alert("fail :-(");
  }

});

service:

Public Class tobject
    Public Property frmid As String
    Public Property frmval As String
End Class


<OperationContract>
<WebInvoke(ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
Public Function SendItems(anyobjectname As List(Of tacosobjectitem)) As Boolean

    Return True

End Function

This now turns the anyobjectname into a list of my object. I cant understand why i couldnt use BodyStyle:- Wrapped but it works so im happy. Hope it helps someone out there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top