我有一个关于Ajax的问题总是得到“textStatus:parsererror,errorThrown:SyntaxError:Unexpected token:”,
但是,响应是“responseText:{”成功“:”搜索成功“,”时间表“:”aaa“},”
我在网站jsonlint.com上搜索它显示JSON是有效的。
注意:“aaa”是我想要返回的字符串,我想它可能太长了所以将其改为“aaa”,但错误仍然存在。
以下是Ajax的代码
$.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");
}
}
})
这是Perl的
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;
答案 0 :(得分:0)
不要编写自己的JSON,而是让perl为你编码。
use JSON::PP;
my $response = (@info)?
{"success" => "Search Successful", "Timetable" => @info}:
{"error" => "Search Error"};
my $json = JSON::PP->new->allow_nonref;
print $json->encode($response);