我正在使用 jquery get 来调用 ASP MVC Controller 。它返回部分 View 。即一堆 html
如果出现错误,我想为用户填充一些信息,但 ASP MVC 正在发送回整个页面,因此我需要从中获取文本。
我已经尝试过:
$('#edit').ajaxError(function (e, xhr, settings, exception) {
var item = xhr.responseText.text();
var response = item.match(/.*<body.*>(.*)<\/body>.*/);
if (!response) {
$(this).html('Error: ' + xhr.status + ' Message:' + xhr.statusText);
}
else {
$(this).html(response);
};
});
但是我得到 Uncaught TypeError: [followed by thecontents of xhr.responseText] has no method text
如果我直接在responseText上调用match,我会得到null结果。
我猜我有一些根本性的误解,所以如果有人可以提供帮助......
最佳答案
您还应该修改正则表达式以包含换行符,即
var response = xhr.responseText.match(/.*<body.*>([\s\S]*)<\/body>.*/);
关于jquery - 尝试解析 xhr.responseText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6946184/