function (data) {
    //add values based on activity type
    //data = JSON.parse(data);
    //alert(abc.Phone1);

    alert(data.myName)

    alert(data.toString());
    if (activityType == "Phone") {
    }
    return;

},

As you can see this callback function of $.ajax taking JSON data from controller.

For example:

[{"name":"myName" ,"address": "myAddress" }]

在这种情况下,我的第一个alert 给我未定义,第二个/第三个alert 弹出窗口显示:

[{"name":"myName" ,"address": "myAddress" }]

How can I access value by name so that my first alert filled out with myName which is value of name?

推荐答案

除了解析JSON之外,您还可以执行如下操作:

$.ajax({
  ..
  dataType: 'json' // using json, jquery will make parse for  you
});

要访问JSON的属性,请执行以下操作:

data[0].name;

data[0].address;

Why you need data[0] because data is an array, so to its content retrieve you need data[0] (first element), which gives you an object {"name":"myName" ,"address": "myAddress" }.

And to access property of an object rule is:

Object.property

or sometimes

Object["property"] // in some case

所以你需要

data[0].name and so on to get what you want.


If you not

set dataType: json then you need to parse them using $.parseJSON() and to retrieve data like above.

Jquery相关问答推荐

如何使用 JQuery Select 没有特定子元素的元素

在 textarea 输入中美化 json 数据

在学习 jQuery 之前学习 JavaScript 是个好主意吗?

Bootstrap 4 多选下拉菜单

如何在 jQuery Select 器中定义 css :hover 状态?

如何使用jQuery找到元素顶部的垂直距离(以px为单位)

jQuery-UI 的自动完成不能很好地显示,z-index 问题

如何使用 jQuery 进行带命名空间的 XML 解析

addClass - 可以在同一个 div 上添加多个类吗?

如何将文本从 div 复制到剪贴板

将图像 url 转换为 Base64

如何获得元素的正确偏移量? - jQuery

使用 JQuery 激活 bootstrap 选项卡

Bootstrap datepicker Select 后隐藏

jQuery位置href

如何淡出显示:inline-block

为什么要使用发布/订阅模式(在 JS/jQuery 中)?

'touchstart' 事件是否有与点击事件相同的 e.PageX 位置?

JQuery 判断 DOM 中的重复 id

AJAX 成功中的 $(this) 不起作用