0

Does anyone know why I can't access the array keys in the generated json array. Basically, I am doing an ajax call, then on success, doing another one. Everything works fine and I am getting a full, healthy and valid json array return, but I cannot access the keys and values.

My jQuery

$("#register-form").submit(function(e){
  e.preventDefault();
  var firstname = $('#regfirstname').val();
  var surname = $('#regsurname').val();
  var email = $('#regregemail').val();
  var password = $('#regregpassword').val();
  var band = $('#regband').val();
  var website = $('#regwebsite').val();
  var company = $('#regcompany').val();
  var address = $('#regaddress').val();
  var city = $('#regcity').val();
  var state = $('#regstate').val();
  var postcode = $('#regpostcode').val();
  var country = $('#regcountry').val();
  var phone = $('#regphone').val();
  var age = $('#regage').val();
  var subscribe = $('#regsubscribe').val();
  $.ajax({
    type : 'POST',
    url : '/ajax/register',
    data : 'firstname='+firstname+'&surname='+surname+'&email='+email+'&password='+password+'&band='+band+'&website='+website+'&company='+company+'&street='+address+'&city='+city+'&state='+state+'&postcode='+postcode+'&country='+country+'&phone='+phone+'&age='+age+'&subscribe='+subscribe,
    success : function(data){
      // automatically log the user in
      $.ajax({
        type : 'POST',
        url : '/ajax/login',
        data : 'email='+email+'&password='+password,
        success : function(user){
          alert(user.logged_in);
        },
        datatype : 'json'
      });
    },
    datatype : 'json'
  });
});

If I do alert(user); it alerts the entire json array. But as soon as i try and access a value within it, it returns undefined.

Example json array:

{"logged_in":true,"firstname":"Joe","surname":"Bloggs","Full_name":"Joe Bloggs","email":"[email protected]","phone":"123456789","website":"www.site.com.au","age":"25","street":"1 Road Street","city":"Town","state":"BLA","postcode":"1234","country":"13","company":"Freedman Electronics","band":"Daysend","subscribe":"2","mics":0}

Any ideas?

Is this even possible to do? (AJAX within AJAX I mean).

4
  • When you say alert(user) shows the entire json array, do you mean it's showing: {"logged_in":true,"firstname":"Joe","surname":"Bloggs", … because it should show: [Object] - is the 'user' parameter being populated by a string instead of the json object? Commented Apr 14, 2011 at 5:50
  • Yep, that's right, it outputs the entire array. Commented Apr 14, 2011 at 5:51
  • It's not an answer to your question, but why don't you login the user with the first reuqest, if registration was successfull. This way you have two requests, which is slower. Commented Apr 14, 2011 at 6:02
  • When you get the response back, is there a JSON tab available in the Firebug(if using firefox) panel..and I do agree with dioslaska you should complete it in single ajax request... Commented Apr 14, 2011 at 6:08

3 Answers 3

2

Use dataType instead of datatype in your ajax settings. It's case sensitive.

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

1 Comment

I totally missed the obvious. +1
0

Try downloading json2.js and add it in your app

and try the following code in the success block

success : function(user){{
      var response=eval("("+JSON.stringify(user)+")");
      alert(response.logged_in);
      alert(response.firstname);
},

Comments

0

The problem seems to lie with your /ajax/login page - the response is being returned as a string, not a json object. For example the following:

<script type="text/javascript">
var test = {"logged_in":true,"firstname":"Joe","surname":"Bloggs","Full_name":"Joe Bloggs","email":"[email protected]","phone":"123456789","website":"www.site.com.au","age":"25","street":"1 Road Street","city":"Town","state":"BLA","postcode":"1234","country":"13","company":"Freedman Electronics","band":"Daysend","subscribe":"2","mics":0};

alert(test);
</script>

Displays:

[object Object]

If in your case the login page is returning the following for alert(user):

{"logged_in":true,"firstname":"Joe","surname":"Bloggs","Full_name":"Joe Bloggs","email":"[email protected]","phone":"123456789","website":"www.site.com.au","age":"25","street":"1 Road Street","city":"Town","state":"BLA","postcode":"1234","country":"13","company":"Freedman Electronics","band":"Daysend","subscribe":"2","mics":0}

Then you either need to debug the output ContentType coming from the login page (best), or do something like:

var jsonuser = JSON.parse(user);
alert(jsonuser.logged_in);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.