0

asmx page where I call a web service and it returns a JSON string, but my jQuery post is failing and I am not sure why?

                    $.post("/DesktopModules/EastlinkMilestones/MilestonesService.asmx /InsertYear_Decade",
                {   Year: $('#txtYear_Decade').val(),
                    IsDecade: $('#chkIsDecade').prop('checked')
                },
                function (data)
                {
                    Current_Edit_State == EDIT_STATE.UPDATE;
                    successMessage.text('Year/Decade created successfully.');
                    informationMessage.css('display', 'none');
                    successMessage.css('display', 'block');                        
                },
                "json"
                )
                 .done(function(data) {
                    alert( "second success" );
                    alert( JSON.stringify(data));
                    })
                 .fail(function(jqXHR, textStatus, errorThrown) {                     
                     alert( "error" );
                     alert( JSON.stringify(jqXHR));
                     alert(textStatus);
                     alert(errorThrown);
                    })
                 .always(function() {
                    alert( "finished" );
                 });

.ASMX Method

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string InsertYear_Decade()
    { var Request = HttpContext.Current.Request;
        var Response = HttpContext.Current.Response;

                    var js = new JavaScriptSerializer();

                string mileStoneData = js.Serialize(data);

                return mileStoneData; // {"ID": 20, "Year": "2013", "IsDecade": false }
            }
    }

The web method always fails and it's textStatus = parsererror and errorThrown = Invalid Character?

Hunter
  • 574
  • 2
  • 6
  • 27

2 Answers2

1

You need to NOT returning value, just wirte in the response. Try something like this:

JavaScriptSerializer serializer = new JavaScriptSerializer();
Response.ContentType = "application/json";
Response.Write(serializer.Serialize(data));
frikinside
  • 1,244
  • 1
  • 9
  • 19
-1

Instead of returning a type string, you should return the object directly. No need to use JavascriptSerializer, and in your jquery post set your contentType as application/json

sandeep
  • 149
  • 1
  • 12