Undefined message tacked onto the start of a string
Hi.
I am trying to loop through a json result to create a table which I can do fine. The problem I am having is that at the start of the string I have the 'undefined' tacked to the start of it and I cant see where it is coming from.
this is my code.
my json result set is: {"errorIsSet":"1","results":["The title must not be empty","Cost is not correct"]}
Code JavaScript:
var obj = jQuery.parseJSON(data);
if(obj.errorIsSet==1)
{
var i;
var errorImage = '<img src="images/warning.gif" alt="Warning!" width="24" height="24" style="float:left; margin: -5px 10px 0px 0px; " />';
var tableMiddle;
var tableTop = '<table width="100%" border="0" cellspacing="0" cellpadding="0">' ;
for(var i in obj.results)
{
tableMiddle += '<tr><td>'+errorImage+'</td><td>'+obj.results[i]+'</td></tr>';
alert (tableMiddle);
}
var tableBottom = '</table>';
var errorTable = tableTop+tableMiddle+tableBottom;
alert (errorTable);
$("div.error").empty(this).append(errorTable);
$("div.error").show();
}
And this is the resultant html
Code HTML4Strict:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
undefined<tr><td><img src="images/warning.gif" alt="Warning!" width="24" height="24" style="float:left; margin: -5px 10px 0px 0px; " /></td>
<td>The title must not be empty</td></tr>
<tr><td><img src="images/warning.gif" alt="Warning!" width="24" height="24" style="float:left; margin: -5px 10px 0px 0px; " /></td>
<td>Cost is not correct</td></tr></table>
Thanks for your help.
Chris