0

Here's my AJAX call in my CSHTML file:

var personId = "Somebody";
var personNotes = "E, F Sharp, A Flat";

var TheData = { "D": { "Id": personId, "Notes": personNotes} };

$.ajax({
    type: "POST",
    url: "/DataHandler.svc/PostResult",
    data: TheData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        $(ValidationErrorDiv).html("It works");
    },
    error: function (HelpRequest, ErrorCode, TheError) {
        $(ValidationErrorDiv).html("It doesn't work - " + TheError);
    }
});

And here's what is in DataHandler.svc.cs:

public class PostResultType
{
    public String Id;
    public String Notes;
}

// in the interface:

{   
  [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
  int PostResult(PostResultType D);
}

// in the class:

public int PostResult(PostResultType D)
{
    return 1;
}

If this is called as is, the Ajax call returns an Internal Server Error without getting to the C# code.

If I replace "data: TheData" with "data: JSON.stringify(TheData)" in the Ajax call, it reaches the C# code, but D.Id and D.Notes are both null.

If I replace "PostResultType D" with "String Id, String Notes" as PostResult's parameters, all of the calls in DataHandler.svc.cs start throwing Internal Server Errors. Apparently, calls using Post are being limited to one parameter.

Yes, using Get instead of Post will work, but the ultimate goal is to use this for updating a database, so I would prefer to get Post to work.

Don Del Grande
  • 411
  • 6
  • 20
  • You still have to serialize the object into a JSON string: http://stackoverflow.com/questions/6323338/jquery-ajax-posting-json-to-webservice – Blender Oct 23 '12 at 17:38
  • Any reason you're not using the ASP.NET Ajax client-side framework? It does all this stuff for you.. – Mike Christensen Oct 23 '12 at 17:44

0 Answers0