Neo
Neo

Reputation: 3

using jquery to load json

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:

loaduser(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 ;)

Upvotes: 0

Views: 208

Answers (1)

Daff
Daff

Reputation: 44215

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", }';

Upvotes: 1

Related Questions