运行嵌套的ajax请求时无法访问json数组元素

时间:2011-04-14 05:41:49

标签: jquery ajax json

有谁知道为什么我无法访问生成的json数组中的数组键。基本上,我正在做一个ajax调用,然后是成功,再做一个。一切正常,我得到一个完整,健康和有效的json数组返回,但我无法访问键和值。

我的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'
  });
});

如果我alert(user);,它会警告整个json数组。但是一旦我尝试访问其中的值,它就会返回undefined。

示例json数组:

{"logged_in":true,"firstname":"Joe","surname":"Bloggs","Full_name":"Joe Bloggs","email":"address@email.com","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}

有什么想法吗?

这甚至可以吗? (AJAX中的AJAX我的意思是)。

3 个答案:

答案 0 :(得分:2)

在ajax设置中使用dataType而不是datatype。这是区分大小写的。

答案 1 :(得分:0)

问题似乎在于你的/ ajax / login页面 - 响应是以字符串形式返回的,而不是json对象。例如以下内容:

<script type="text/javascript">
var test = {"logged_in":true,"firstname":"Joe","surname":"Bloggs","Full_name":"Joe Bloggs","email":"address@email.com","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>

显示器:

[object Object]

如果在您的情况下登录页面返回以下警报(用户):

{"logged_in":true,"firstname":"Joe","surname":"Bloggs","Full_name":"Joe Bloggs","email":"address@email.com","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}

然后你需要调试来自登录页面的输出ContentType(最好),或者执行以下操作:

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

答案 2 :(得分:0)

尝试下载json2.js并将其添加到您的应用中

并在成功块中尝试以下代码

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