Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I not able to pass a JSON object to a WCF web service?
    text
    copied!<p>I built a fairly complex app using the older "ASMX" web service implementation and I am migrating to WCF. There is one thing that is just driving me crazy - it <em>should</em> be easy but my AJAX calls error out no matter how I try to structure this call. This worked fine with ASMX calls but not with WCF.</p> <p>Here is the AJAX call:</p> <pre><code> var ProfileData = new Object(); ProfileData.SUID = SUID; ProfileData.FirstName = $("#FirstName").val(); ProfileData.LastName = $("#LastName").val(); ProfileData.Birthdate = new Date($("#Birthdate").val()); var DTO = {'ProfileData': ProfileData }; $.ajax({ type: "POST", url: "AllianceService.svc/SaveBasicProfile", data: JSON.stringify(DTO), contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { UpdateTips($("#BasicProfileValidate"), "Success! Your data has been updated."); } }, error: function (xhr, ajaxOptions, thrownError, request, error) { alert('Error Saving Basic Profile Data'); } }); </code></pre> <p>Here is the declaration of the type on the C# / Server side:</p> <pre><code>[DataContract] public class BasicFolderData { [DataMember] public string SUID { get; set; } [DataMember] public string FirstName { get; set; } [DataMember] public string LastName { get; set; } [DataMember] public DateTime Birthdate { get; set; } } </code></pre> <p>And here is the definition of the service:</p> <pre><code> [OperationContract] public int SaveBasicProfile(BasicFolderData ProfileData) { ... do stuff } </code></pre> <p>Now, if I break out all of the datamembers and use them as parameters, I can get this to work with a simple </p> <pre><code> data: JSON.stringify(ProfileData) </code></pre> <p>But the object is actually much longer than this and I'd like to know how to pass objects. </p> <p>Also, I have tried:</p> <pre><code> data: JSON.stringify({"ProfileData": ProfileData }), </code></pre> <p>and</p> <pre><code> data: JSON.stringify('{"ProfileData":' + ProfileData + '}'), </code></pre> <p>and</p> <pre><code> data: '{"ProfileData":' + JSON.stringify(ProfileData) + '}', </code></pre> <p>but all to no avail...I get the error message on all of them.</p> <p>If I use: </p> <pre><code> data: JSON.stringify(ProfileData), </code></pre> <p>then, oddly enough, the request makes it to the server but the ProfileData parameter is null. I suspect that the problem is my datacontract but don't know where to start. Also, I have a lot of functions with scalar parameters that work just fine - it is only when I attempt to pass objects that I fail. Any help would be greatly appreciated!!</p> <p>Following DonAndre's suggestion, I have found that simply changing the parameter to a scalar type will make the call succeed. However, the parameter is always null.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload