简体   繁体   English

获取json数据响应?

[英]Get json data response?

From this 由此

$.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
    }
})

I get this in browser console: 我在浏览器控制台中得到这个:

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

Can anybody tell me how can I access all data according to response that is in individual variables? 有人可以告诉我如何根据单个变量中的响应访问所有数据吗?

it not dateType:'json', it should be dataType:'json' 它不是dateType:'json',应该是dataType:'json'

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

hence your JSON returned in treated as string and not object. 因此,您的JSON返回为字符串而不是对象。 you can however use JSON.parse() but fixing the dateType to dataType should work 您可以使用JSON.parse()但将dateType固定为dataType应该可以

Correct the mistake shown by @bipen and in success function : 纠正@bipen和成功函数中显示的错误:

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

Working Fiddle : Fiddle 工作小提琴: 小提琴

Try this, 尝试这个,

In View: 在视图中:

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

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

Controller: 控制器:

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