У меня есть этот код
$(document).ready(function(){
// ...
$('form').submit(function() {
// ...
$.ajax({
type: "GET",
url: "/cgi-bin/ajax.pl",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: $(this).serialize(),
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('div#create_result').text(
"responseText: " + XMLHttpRequest.responseText +
", textStatus: " + textStatus +
", errorThrown: " + errorThrown);
$('div#create_result').addClass("error");
}, // error
success: function(result){
if (result.error) { // script returned error
$('div#create_result').text("result.error: " + result.error);
$('div#create_result').addClass("error");
} // if
else { // perl script says everything is okay
$('div#create_result').text(
"result.success: " + result.success +
", result.userid: " + result.userid);
$('div#create_result').addClass("success");
} //else
} // success
}); // ajax
$('div#create_result').fadeIn();
return false;
});
});
, которая всегда выдает сообщения об ошибках, если она прошла успешно.
Пример:
responseText: Content-Type: application/json; charset=utf-8 {"success" : "Activity created", "Activity number" : "38"}, textStatus: parsererror, errorThrown: SyntaxError: JSON.parse
Есть идеи, что не так?
Обновление
Вот как я создаю строки JSON в серверном сценарии Perl.
...
$json = qq{{"error" : "Owner $owner doesn't exist"}};
...
$json = qq{{"success" : "Activity created", "Activity number" : "$id"}};
...
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;