1

I am trying to fill a variable in this format using JQuery Ajax but because of all the brackets, I am unable to return the data in the format from the backend

elements = [{ 
    key: 1, 
    label: "Food", 
    open: false, 
    children: [
        { key: 211, label: "Burger" }
    ]
}];

Here is the code I am currently using which isn't working

menu.aspx:

$.ajax({
    type: "POST",
    url: "menu.aspx/get_menu",
    data: {},
    contentType: "application/json",
    dataType: "json",
    success: function (msg) {
        $("#test").html(msg.d);
    }
});

backend:

[WebMethod(EnableSession = false)]
public static string get_menu()
{
    return "hello world";
}
3
  • 5
    You are returning a string, not JSON. Try $("#test").html(msg); Commented Dec 12, 2014 at 15:05
  • Separately, if you specify contentType on your $.ajax call, you're responsible for serializing the data you're sending from the client to the server in that form (e.g., you can't do data: {}). jQuery only does serialization for you with the standard URI-encoded form. Unless you're sending JSON to the server, remove contentType: "application/json". Commented Dec 12, 2014 at 15:11
  • Your backend script surely won't give you the data you expect, only "hello world" :) Commented Dec 12, 2014 at 15:32

1 Answer 1

0

Your backend code should look something more like this (WebService.cs in my example, which is the CodeBehind for WebService.asmx ):

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    public class JsonResult
    {
        public string key, label, open;
        public List<Children> children;
    }
    public class Children
    {
        public string key, label;
    }

    [WebMethod]
    public List<JsonResult> test()
    {
        List<Children> child = new List<Children>();
        child.Add(new Children
        {
            key = "211",
            label = "Burger"
        });

        List<JsonResult> result = new List<JsonResult>();
        result.Add(new JsonResult
        {
            key = "1",
            label = "Food",
            open = "false",
            children = child
        });
        return result;
    }
}

This code sends the created list as JSON - which looks like this when jQuery receives it:

{"d":[
    {
        "__type":"WebService+JsonResult",
        "key":"1",
        "label":"Food",
        "open":"false",
        "children":[
            {
                "key":"211",
                "label":"Burger"
            }
        ]
    }
]}

Then, the jQuery AJAX is very simple, like so:

$.ajax({
    type: "POST",
    url: "/WebService.asmx/test",
    contentType: "application/json",
    success: function (response) {
        var jsonResponse = response.d;
    }
});

Now you can access each element as native JavaScript objects, like I show here in the success callback when assigning each object to a variable. Now you can do anything you want to with the data.

success: function (response) {
    var jsonResponse = response.d;
    for (var i = 0; i < jsonResponse.length; i++) {
        var key = jsonResponse[i].key;
        var label = jsonResponse[i].label;
        var open = jsonResponse[i].open;
        var children = jsonResponse[i].children;
        for (var a = 0; a < children.length; a++) {
            var key = children[a].key;
            var label = children[a].label;
        }
    }
}

This is 100% tested and working. Let me know if there's anything else I can help you with.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.