开发者

using jquery to load json

开发者 https://www.devze.com 2023-02-05 23:10 出处:网络
I have a variable on a page like this: var userdetails = \"{ \'id\': \'1\', \'username\': \'me\', }\"; now, I wanna call a function to load those id and username in some divs

I have a variable on a page like this:

var userdetails = "{ 'id': '1', 'username': 'me', }";

now, I wanna call a function to load those id and username in some divs like this one:

lo开发者_JAVA百科aduser(userdetails)

I used this but didn't work

function loaduser(jsoni){

$.getJSON(jsoni, function(data) { $('#id').html(data.id); $('#username').html(data.username); } }

thank u guys ;)


Use jQuery.parseJSON. Your example won't work though because it is not valid JSON (valid JSON requires double instead of single quotation marks):

var userdetails = '{ "id": 1, "username": "me", }';
0

精彩评论

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