我有一个Ajax函数,用于处理发出警报的一般错误。 我无法理解的是如何测试它。我想以这种方式测试它我将模拟错误。
功能:
function handleAjaxError(xhr, status, error) {
var json;
var errors;
var html;
try {
// Populate errorText with the comment errors
json = $.parseJSON(xhr.responseText);
errors = json['errors'];
} catch (err) {
// If the responseText is not valid JSON (like if a 500 exception was thrown), populate errors with a generic error message.
errors = [];
errors[0] = "Error communicating with the server.";
}
var html = ""
for (var i = 0; i < errors.length; i++) {
html = html + alertError(errors[i]);
}
displayAlerts(html);
}