开发者

Accessing json array by string? (Converting string to var. name)

I'm trying to post the "lang_id" var. to "get_lang.php" with jquery to get (json) data. But I can't access the data.

Now trying to do

  var r = $(this).attr('rel');
  var v = data.r;

But this doesn't work because of "r" is string IMO.

Also tried

data.window[r] // but...

"get_lang.php";

$lang_id = (int) ($_POST['lang_id']);

if($lang_id == 1)
{
     $lang['simple'] = 'aaa';
     $lang['array'] = 'bbb';
}

if($lang_id == 2)
{
     开发者_开发百科$lang['simple'] = 'ccc';
     $lang['array'] = 'ddd';
}

print json_encode($lang);

my.js;

$.post("get_lang.php", { "lang_id": 2}, function(data){

   $('.lang').each(function() {

     var r = $(this).attr('rel');
     var v = data.r;

     $(this).text(v);

    });

},"json");

thanks for help.


Try

var v = data[r];

The dot notation interprets r as a string and not as a variable.


I think maybe you need to tell jQuery the right content type. Put this at the TOP of your get_lang.php file

header("Content-Type: application/json");

And see if jQuery likes it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜