繁体   English   中英

获取json数据响应?

[英]Get json data response?

由此

$.ajax(
{
    type: 'post',
    dateType: 'json',
    url: '<?php echo base_url(); ?>buyer/get_address',
    data: { data: $id },
    success: function (resp) {
        alert(resp); //retuns whole result
        alert(resp.address_id); //returns undefind
    }
})

我在浏览器控制台中得到这个:

{
  "address_id": "10",
  "user_id": "81",
  "address_as": "wholesale",
  "receiver_name": "Karamjeet Singh",
  "address": "street no 1,",
  "zip_code": "11111111",
  "phone": "222222222222",
  "province": "14",
  "regency": "Bali",
  "sub_district": "",
  "status": "1",
  "created": "28 Oct 2013 , 11 : 06 : 06 am",
  "updated": "28 Oct 2013 , 11 : 06 : 06 am"
}

有人可以告诉我如何根据单个变量中的响应访问所有数据吗?

它不是dateType:'json',应该是dataType:'json'

  ....
  type : 'post',
  dateType:'json',
 //^^^^^^---here 
  ...

因此,您的JSON返回为字符串而不是对象。 您可以使用JSON.parse()但将dateType固定为dataType应该可以

纠正@bipen和成功函数中显示的错误:

success: function(resp)
{
       alert(resp[0].address_id);
}

工作小提琴: 小提琴

尝试这个,

在视图中:

$('#mybtn').click(function (e) {
            $.ajax({
                type: 'post',
                dateType: 'json',
                url: '../Home/test',
                data: {  },
                dataType: 'json',                
                success: function (resp) {

                    alert(resp.val); //returns undefind
                }
            })
        });

控制器:

public class t
    {
        public int MyProperty { get; set; }
        public string val { get; set; }
    }
[AcceptVerbs(HttpVerbs.Post)]
        public JsonResult test()
        {
            var dataitem = new t { MyProperty =1,val="jeet" };
            return Json(dataitem, JsonRequestBehavior.AllowGet);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM