0

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?

2
  • What are you getting for alert(resp.d.address_id) ? Commented Oct 28, 2013 at 7:02
  • 5
    There's a typo in your ajax configuration. dateType:'json', should be dataType: 'json' Commented Oct 28, 2013 at 7:03

3 Answers 3

4

it not dateType:'json', it should be dataType:'json'

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

hence your JSON returned in treated as string and not object. you can however use JSON.parse() but fixing the dateType to dataType should work

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks sir .actually i am using IDE .in this when i put dateType:'json' it converted all vars in green color so i not actully see it carefully.thanks again sir for rectify my fault so rapidly.its working fine after changing .
Sir your Profile pic is so dangrous.what intimation it depicts? :)
1

Correct the mistake shown by @bipen and in success function :

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

Working Fiddle :Fiddle

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.