0

I am trying to post a form to my web service with no great success so far.
Before posting to the server I am converting the form to an object. I get an error when posting the form Object to my Asmx web service.

My Ajax:

 var formObject = $(form).serializeObject();
 var formData = JSON.stringify(formObject);
 $.ajax({
          type: "POST",
          url: "./Service.asmx/PostAutomaticRule",
          data: { myObject: formData },
          contentType: "application/json; charset=utf-8",
          dataType: "json",
                success: function (response) {
                    alert("success");
                },
                error: function (response) {
                    alert("Error");
                }
            });

My asmx:

  [WebMethod(EnableSession = true)]
    public void PostAutomaticRule(MyClass myObject)
    {
        Debug.WriteLine(myObject);
    }


}

[Serializable]
public class MyClass
{
    public string automaticRuleName { get; set; }
    public string action { get; set; }
    public string increaseBudgetByValue { get; set; }
    public string increaseBudgetMaximumBudget { get; set; }
}

More information: When debugging these are the values:

enter image description here

What am I doing wrong?

Offir
  • 3,252
  • 3
  • 41
  • 73

3 Answers3

0

Please try the below code for the data parameter in your ajax call:

data: {myObject: JSON.stringify($(form).serializeObject())},
Alan Jurczak
  • 479
  • 5
  • 14
0

Based on sending-json-collection-to-asmx-webservice

I'll say change to:

data: {Object : JSON.stringify($(form).serializeObject())}

or

data: {myObject : JSON.stringify($(form).serializeObject())}

But you might have to create a more specific class.

Community
  • 1
  • 1
Guillaume Kiz
  • 443
  • 4
  • 10
0

Finally I got it like this:

$.ajax({
          type: "POST",
          url: "./Service.asmx/PostAutomaticRule",
          data: "{'myObject': "+formData+"}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
                success: function (response) {
                    alert("success");
                },
                error: function (response) {
                    alert("Error");
                }
            });
Offir
  • 3,252
  • 3
  • 41
  • 73