簡體   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