最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax request returns undefined - Stack Overflow

programmeradmin1浏览0评论

I have the following ajax request:

$.ajax({
    type:'POST',
    url:'../php/anmelden_info.php',
    data:{
        kat :  'Freizeitfussballer'
    },
    success: function(data){
        alert(data[0].anmelde_info);
    }
});

which alerts undefined but i don't know why, because when I go the my console and look for the answer I get:

[{"kategorien_id":"3","kat_name":"Fasnacht","anmelde_info":"\n<b>Informationen:.... ......<\/b>\n<br \/>\nDer Turniereinsatz betr\u00e4gt 100.- pro Mannschaft."}]

and I have no idea what I'm doing wrong!

I have the following ajax request:

$.ajax({
    type:'POST',
    url:'../php/anmelden_info.php',
    data:{
        kat :  'Freizeitfussballer'
    },
    success: function(data){
        alert(data[0].anmelde_info);
    }
});

which alerts undefined but i don't know why, because when I go the my console and look for the answer I get:

[{"kategorien_id":"3","kat_name":"Fasnacht","anmelde_info":"\n<b>Informationen:.... ......<\/b>\n<br \/>\nDer Turniereinsatz betr\u00e4gt 100.- pro Mannschaft."}]

and I have no idea what I'm doing wrong!

Share Improve this question edited Mar 28, 2013 at 8:26 Beetroot-Beetroot 18.1k3 gold badges39 silver badges44 bronze badges asked Mar 28, 2013 at 8:20 fsulserfsulser 9042 gold badges11 silver badges36 bronze badges 2
  • Try to console.log(data) and use console to debug and see what exactly e in data object. – Pouki Commented Mar 28, 2013 at 8:25
  • 1 Before you try and access the data, do data = JSON.parse(data) and see if that helps. – adeneo Commented Mar 28, 2013 at 8:28
Add a ment  | 

3 Answers 3

Reset to default 3

set dataType: json in you jquery call
As follows

 $.ajax({
    type:'POST',
     url:'../php/anmelden_info.php',
     data:{ kat :  'Freizeitfussballer'},
     dataType: json,
     success: function(data){
      console.dir(data);
     }
    });

or if it not worked try this

 $.ajax({
    type:'POST',
     url:'../php/anmelden_info.php',
     data:{ kat :  'Freizeitfussballer'},
     dataType: json,
     success: function(data){
     data = JSON.parse(data);
     console.dir(data);
     }
    });
Try the below code first whilst you have the developer tools open in either Firebug or Chrome.  This will show you the structure of the data obejct passed into the sucess method.

$.ajax({
    type:'POST',
     url:'../php/anmelden_info.php',
     data:{
      kat :  'Freizeitfussballer'
     },
     success: function(data){
      console.dir(data);
     }
    });

You have to use $.each() in your success function:

success: function(data){
    $.each(data, function(i, item){
       console.log(data.anmelde_info);
    });
}

And i suggest you to use console.log() for a better debugging.

发布评论

评论列表(0)

  1. 暂无评论