An issue I am facing is that the Ajax always returns "textStatus: parsererror, errorThrown: SyntaxError: Unexpected token :".
However, the response actually shows as "responseText: {"success":"Search Successful","Timetable":"aaa"},".
I checked on jsonlint.com and it confirmed the JSON validity.
Note that "aaa" was the desired string to be returned. It may have been too long, so I changed it to "aaa", but the error persists.
Below is the Ajax code:
$.ajax({
type:"Get",
url:"cgi-bin/timetable.pl",
contentType:"application/json;charset=utf-8",
dataType:"jsonp",
data:"username="+username,
error:function(XMLHttpRequest,textStatus,errorThrown)
{
$('div#result').text(result);
$('div#result').text("responseText: " + XMLHttpRequest.responseText
+ ", textStatus: " + textStatus
+ ", errorThrown: " + errorThrown);
$('div#result').addClass("error");
},
success: function(data)
{
if (data.error)
{
$('div#result').text("data.error: " + data.error);
$('div#result').addClass("error");
}
else
{
$('div#result').text("data.success: " + data.success
+ ", data.userid: " + data.clasinfo);
$('div#result').addClass("success");
}
}
})
And here is Perl's:
foreach $classid(@claid)
{
$class->execute($classid);
while (@cinfo = $class->fetchrow_array())
{
$num = @cinfo;
$combineinfo = "";
for ($i=0;$i<$num;$i++)
{
$combineinfo .= $cinfo[$i]."V";
}
}
push(@info,$combineinfo);
}
$json = (@info)?
qq{{"success":"Search Successful","Timetable":"'@info"}}:
qq{{"error":"Search Error"}};
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;