Ajax MVC部分返回正确的响应然后触发错误处理程序

时间:2022-10-08 09:16:44

This has me completely stumped. So odd.

这让我完全难过。太奇怪了。

I have this Ajax function defined:

我定义了这个Ajax函数:

$.ajax({
    type: 'GET',
    dataType: 'text/HTML',
    url: getLicenseeDetailsUrl,
    success: function (response) {
        $('#licenseeDetails').html('');
        $('#licenseeDetails').html(response);
    },
    error: function (xhr) {
        alert('Failed to get licensee details');
    }
});

And I have it calling into my controller which has an action like:

我让它调用我的控制器,它有一个动作,如:

public ActionResult LoadLicenseeDetails(long licenseeId)
{
    var model = new LicenseeDetailsViewModel();

    var licencesee = _licensingRepository.LoadById(licenseeId);
    var licenses = _licensingRepository.LoadLicenses(licenseeId);

    model.Licencee = Mapper.Map<Licensee, LicenceeViewModel>(licencesee);
    model.Licences = Mapper.Map<IEnumerable<License>, IEnumerable<LicenceViewModel>>(licenses);

    return this.PartialView("_LicenseeDetails", model);
}

This all seems to be working as expected without any errors, however it ends up firing the Ajax error function, not the success function.

这一切似乎都按预期工作,没有任何错误,但它最终会触发Ajax错误函数,而不是成功函数。

Looking at the xhr.responseText I can see the correct response information from the action controller!!

看一下xhr.responseText,我可以看到动作控制器的正确响应信息!

All with a status 200 OK as well. What on earth am I doing wrong here?

所有状态均为200 OK。我到底在做什么呢?

1 个解决方案

#1


9  

What on earth am I doing wrong here?

我到底在做什么呢?

this:

dataType: 'text/HTML'

should become:

dataType: 'html'

Quote from the documentation of the dataType parameter:

引用dataType参数的文档:

dataType (default: Intelligent Guess (xml, json, script, or html))

dataType(默认值:Intelligent Guess(xml,json,script或html))

Type: String

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

您期望从服务器返回的数据类型。如果没有指定,jQuery将尝试根据响应的MIME类型推断它(XML MIME类型将产生XML,在1.4 JSON中将产生一个JavaScript对象,在1.4脚本中将执行脚本,其他任何东西将是以字符串形式返回)。可用的类型(以及作为成功回调的第一个参数传递的结果)是:

"xml": Returns a XML document that can be processed via jQuery.

“xml”:返回可以通过jQuery处理的XML文档。

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

“html”:以纯文本形式返回HTML;包含的脚本标记在插入DOM时进行评估。

"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.

“script”:将响应评估为JavaScript并将其作为纯文本返回。通过将查询字符串参数“_ = [TIMESTAMP]”附加到URL来禁用缓存,除非缓存选项设置为true。注意:这会将POST转换为GET以获取远程域请求。

"json": Evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

“json”:将响应计算为JSON并返回JavaScript对象。 JSON数据以严格的方式解析;任何格式错误的JSON都会被拒绝,并抛出一个解析错误。从jQuery 1.9开始,空响应也被拒绝;服务器应该返回null或{}的响应。 (有关正确的JSON格式的更多信息,请参阅json.org。)

"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.

“jsonp”:使用JSONP加载JSON块。添加一个额外的“?callback =?”到URL的末尾以指定回调。通过将查询字符串参数“_ = [TIMESTAMP]”附加到URL来禁用缓存,除非缓存选项设置为true。

"text": A plain text string.

“text”:纯文本字符串。

multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

多个空格分隔值:从jQuery 1.5开始,jQuery可以将dataType从Content-Type标头中收到的数据转换为您需要的数据类型。例如,如果要将文本响应视为XML,请对dataType使用“text xml”。您还可以发出JSONP请求,将其作为文本接收,并由jQuery解释为XML:“jsonp text xml”。类似地,诸如“jsonp xml”之类的速记字符串将首先尝试从jsonp转换为xml,如果失败,则从jsonp转换为text,然后从text转换为xml。

Or even better, simply get rid of this parameter. jQuery is intelligent enough to use the Content-Type response HTTP header set by the server in order to deduce the correct type and process the parameter passed to the success callback.

或者甚至更好,只需摆脱这个参数。 jQuery足够智能,可以使用服务器设置的Content-Type响应HTTP标头,以推断出正确的类型并处理传递给成功回调的参数。

Look at the Console tab of your javascript debugging toolbar in the browser. It will provide you with more information about the error.

查看浏览器中javascript调试工具栏的Console选项卡。它将为您提供有关错误的更多信息。

#1


9  

What on earth am I doing wrong here?

我到底在做什么呢?

this:

dataType: 'text/HTML'

should become:

dataType: 'html'

Quote from the documentation of the dataType parameter:

引用dataType参数的文档:

dataType (default: Intelligent Guess (xml, json, script, or html))

dataType(默认值:Intelligent Guess(xml,json,script或html))

Type: String

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

您期望从服务器返回的数据类型。如果没有指定,jQuery将尝试根据响应的MIME类型推断它(XML MIME类型将产生XML,在1.4 JSON中将产生一个JavaScript对象,在1.4脚本中将执行脚本,其他任何东西将是以字符串形式返回)。可用的类型(以及作为成功回调的第一个参数传递的结果)是:

"xml": Returns a XML document that can be processed via jQuery.

“xml”:返回可以通过jQuery处理的XML文档。

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

“html”:以纯文本形式返回HTML;包含的脚本标记在插入DOM时进行评估。

"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.

“script”:将响应评估为JavaScript并将其作为纯文本返回。通过将查询字符串参数“_ = [TIMESTAMP]”附加到URL来禁用缓存,除非缓存选项设置为true。注意:这会将POST转换为GET以获取远程域请求。

"json": Evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

“json”:将响应计算为JSON并返回JavaScript对象。 JSON数据以严格的方式解析;任何格式错误的JSON都会被拒绝,并抛出一个解析错误。从jQuery 1.9开始,空响应也被拒绝;服务器应该返回null或{}的响应。 (有关正确的JSON格式的更多信息,请参阅json.org。)

"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.

“jsonp”:使用JSONP加载JSON块。添加一个额外的“?callback =?”到URL的末尾以指定回调。通过将查询字符串参数“_ = [TIMESTAMP]”附加到URL来禁用缓存,除非缓存选项设置为true。

"text": A plain text string.

“text”:纯文本字符串。

multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

多个空格分隔值:从jQuery 1.5开始,jQuery可以将dataType从Content-Type标头中收到的数据转换为您需要的数据类型。例如,如果要将文本响应视为XML,请对dataType使用“text xml”。您还可以发出JSONP请求,将其作为文本接收,并由jQuery解释为XML:“jsonp text xml”。类似地,诸如“jsonp xml”之类的速记字符串将首先尝试从jsonp转换为xml,如果失败,则从jsonp转换为text,然后从text转换为xml。

Or even better, simply get rid of this parameter. jQuery is intelligent enough to use the Content-Type response HTTP header set by the server in order to deduce the correct type and process the parameter passed to the success callback.

或者甚至更好,只需摆脱这个参数。 jQuery足够智能,可以使用服务器设置的Content-Type响应HTTP标头,以推断出正确的类型并处理传递给成功回调的参数。

Look at the Console tab of your javascript debugging toolbar in the browser. It will provide you with more information about the error.

查看浏览器中javascript调试工具栏的Console选项卡。它将为您提供有关错误的更多信息。