JQuery的阿贾克斯充满JavaScript变量问题 [英] JQuery Ajax fills javascript variable issue

查看:107
本文介绍了JQuery的阿贾克斯充满JavaScript变量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以填补使用JQuery的Ajax这种格式的一个变量,但所有的括号,因为,我无法从后台返回的格式的数据。

 元素= [{
    关键:1,
    标签:民以食为天,
    打开:假的,
    孩子:
        {键:211,标签:汉堡}
    ]
}];
 

下面是我目前使用code这是行不通的。

menu.aspx:

  $。阿贾克斯({
    键入:POST,
    网址:menu.aspx / get_menu
    数据: {},
    的contentType:应用/ JSON
    数据类型:JSON,
    成功:函数(MSG){
        $(#考)HTML(msg.d);
    }
});
 

后端:

  [WebMethod的(EnableSession = FALSE)]
公共静态字符串get_menu()
{
    返回的Hello World;
}
 

解决方案

您后端code看起来应该更多同类产品(WebService.cs在我的例子,这是codeBehind为WebService.asmx):

 使用系统;
使用System.Collections.Generic;
使用的System.Web;
使用System.Web.Services;

[WebService的(命名空间=htt​​p://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
[System.Web.Script.Services.ScriptService]
公共类的WebService:System.Web.Services.WebService
{
    公共类JsonResult
    {
        公共字符串键,标签,打开;
        公开名单<儿童>儿童;
    }
    公共类儿童
    {
        公共字符串键,标签;
    }

    [WebMethod的]
    公开名单< JsonResult>测试()
    {
        名单<儿童>孩子=新的名单,其中,儿童>();
        child.Add(新儿童
        {
            关键=211
            标签=汉堡
        });

        名单< JsonResult>结果=新名单,其中,JsonResult>();
        result.Add(新JsonResult
        {
            关键=1,
            标签=民以食为天,
            开=假,
            儿童=小孩
        });
        返回结果;
    }
}
 

这code发送所创建的列表作为JSON - 这看起来是这样的,当jQuery的接收它:

  {D:
    {
        __type:web服务+ JsonResult
        钥匙:1,
        标签:民以食为天,
        开放:假,
        孩子:
            {
                钥匙:211,
                标签:汉堡
            }
        ]
    }
]}
 

然后,jQuery的AJAX是很简单的,就像这样:

  $。阿贾克斯({
    键入:POST,
    网址:/WebService.asmx/test
    的contentType:应用/ JSON
    成功:函数(响应){
        VAR jsonResponse = response.d;
    }
});
 

现在,您可以访问每个元素作为本地JavaScript对象,就像我在这里展示了成功回调分配每个对象变量时。现在,你可以做你想要的任何数据。

 成功:函数(响应){
    VAR jsonResponse = response.d;
    对于(VAR I = 0; I< jsonResponse.length;我++){
        VAR键= jsonResponse [I]。关键;
        VAR标签= jsonResponse [I] .label;
        VAR开放= jsonResponse [I]。开;
        VAR孩子= jsonResponse [I]。孩子;
        对于(VAR一个= 0; A< children.length; A ++){
            VAR键=儿[A]。关键;
            VAR标签=儿[A] .label;
        }
    }
}
 

这是100%测试和工作。让我知道如果有什么我可以帮你。

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";
}

解决方案

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.

这篇关于JQuery的阿贾克斯充满JavaScript变量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆