0

I need some help with my web service and json call.. stuck trying to get the data returned, I've done this successful with strings and objects.. but not an array...

My Web Service Contains:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetSearchCriteria()
{
    var js = new JavaScriptSerializer();
    var obj = HttpContext.Current.Session["mysessionname"];
    if (obj == null)
    {
        var result = new Criteria[] 
        { 
            new Criteria() { Key = Guid.NewGuid(), Operator = "=", ColumnName = "CheckID", Value = "" }
        };

        var serial = js.Serialize(result);
        return serial;
    }
    else
    {
        var serial = js.Serialize((Criteria[])obj);
        return serial;
    }
}

Criteria is:

[Serializable]
public class Criteria
{
    public Guid Key { get; set; }
    public string ColumnName { get; set; }
    public string Operator { get; set; }
    public string Value { get; set; }
}

My Page Contains:

<script type="text/javascript">
    function RefreshCriteria() {
        $.ajax({
            type: 'POST',
            url: '<%= System.Web.VirtualPathUtility.ToAbsolute("~/Services/WebService.asmx/GetSearchCriteria") %>',
            dataType: 'json',
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Content-type",
                           "application/json; charset=utf-8");
            },
            success: function (data) {
                $(data).each(function (i) {
                    var obj = data[i];
                    alert(obj);
                });
            }
        });

    }

    $(document).ready(function () {
        RefreshCriteria();
    });
</script>

What I'm getting is undefined every time.. tried doing

$(data).each(function (i) {
  var obj = data[i];
  alert(obj);
});


$(data).each(function (i, obj) {
  alert(obj);
});

$(data).each(function (i) {
  alert(this);
});

None worked so far..

1 Answer 1

2

you can tighten up the ajax

$.ajax({
   type: 'POST',
   url: '*you need a url here*',
   dataType: 'json',
   success: function (data) {
      $(data).each(function (index, item) {
         console.log(item);
      });
   }
});

next using either firebug or fiddler monitor the request/response to ensure what you are sending and receiving is what you expected.

Sign up to request clarification or add additional context in comments.

3 Comments

When I debug.. the serial variable being returned from the webservice is showing "[{\"Key\":\"07bc31be-be37-4fa8-a144-16e0626c1500\",\"ColumnName\":\"CheckID\",\"Operator\":\"=\",\"Value\":\"\"}]"
ahh.. just got it.. had to do success: function (data) { var obj = jQuery.parseJSON(data.d); $(obj).each(function (i, item) { alert(item.Key); }); }
this is because the result you are returning is a string therefore json string is escaped so it's a literal string. try this return an array of criteria from your webserver (no json reference needed). This should make the data argue in jQuery an array of json objects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.