I currently have a list of items which should be partly filled by an AJAX call. What I have:
<li>Hello, username</li>
The AJAX call pulls the username from a database. So how do you append that pulled information to display it next to "Hello" since anything in the li
tags is straight text?
The AJAX:
ajax({
type: "GET"
url: '../info
success: function (data){
if (data != ''){
var user = info.username;
}
}
})
How would the variable user be put in to the list item tag? can you do something like
<li>Hello, "$user"</li>
If you could have your items formatted like <li>Hello, <span id = "username">username</span>
, it will make your life easier.
Then you could do something like this:
if (data != ''){
var user = info.username;
$('#username').html(user);
}
Does your AJAX request really return a JavaScript object? I suspect it actually returns JSON, so you're going to need to decode it:
$.ajax({
type: "GET",
url: '../info',
success: function(data) {
if(data != '')
{
var info = $.parseJSON(data),
user = info.username;
$('li').text('Hello, '+user);
}
}
});
First, it's probably easier to put a container has the username placeholder,
<li>Hello, <span id="username">username</span></li>
Then, if you're expecting that the ajax request will return a json formatted response, you have to set the dataType
option to get it parsed.
ajax({
type: "GET"
url: '../info',
dataType: 'json',
success: function(data) {
if(data) {
$("#username").text(data.username);
}
}
});
If the username it's returned directly as text:
success: function(data) {
if(data != '') {
$("#username").text(data);
}
}