试图解析xhr.responseText

Pau*_*bra 1 regex ajax jquery xmlhttprequest

我正在使用jquery来调用ASP MVC控制器.返回部分视图.即一堆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);
        };
    });
Run Code Online (Sandbox Code Playgroud)

但我得到Uncaught TypeError:[后跟xhr.responseText的内容]没有方法文本

如果我直接在responseText上调用match,那么我得到null.

我猜我有一些基本的误会,所以如果有人可以帮忙......

Roc*_*oC5 6

您还应该修改正则表达式以包含换行符,即

var response = xhr.responseText.match(/.*<body.*>([\s\S]*)<\/body>.*/);
Run Code Online (Sandbox Code Playgroud)