简体   繁体   English

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

[英]Ajax MVC Partial Returning Correct Response Yet Fires the Error Handler

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. 所有状态均为200 OK。 What on earth am I doing wrong here? 我到底是做错了什么?

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参数的documentation

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). 如果没有指定,jQuery将尝试根据响应的MIME类型推断它(XML MIME类型将产生XML,在1.4 JSON中将产生一个JavaScript对象,在1.4脚本中将执行脚本,其他任何东西将是以字符串形式返回)。 The available types (and the result passed as the first argument to your success callback) are: 可用的类型(以及作为成功回调的第一个参数传递的结果)是:

"xml": Returns a XML document that can be processed via jQuery. “xml”:返回可以通过jQuery处理的XML文档。

"html": Returns HTML as plain text; “html”:以纯文本形式返回HTML; included script tags are evaluated when inserted in the DOM. 包含的脚本标记在插入DOM时进行评估。

"script": Evaluates the response as JavaScript and returns it as plain text. “script”:将响应评估为JavaScript并将其作为纯文本返回。 Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. 通过将查询字符串参数“_ = [TIMESTAMP]”附加到URL来禁用缓存,除非缓存选项设置为true。 Note: This will turn POSTs into GETs for remote-domain requests. 注意:这会将POST转换为GET以获取远程域请求。

"json": Evaluates the response as JSON and returns a JavaScript object. “json”:将响应计算为JSON并返回JavaScript对象。 The JSON data is parsed in a strict manner; JSON数据以严格的方式解析; any malformed JSON is rejected and a parse error is thrown. 任何格式错误的JSON都会被拒绝,并抛出一个解析错误。 As of jQuery 1.9, an empty response is also rejected; 从jQuery 1.9开始,空响应也被拒绝; the server should return a response of null or {} instead. 服务器应该返回null或{}的响应。 (See json.org for more information on proper JSON formatting.) (有关正确的JSON格式的更多信息,请参阅json.org。)

"jsonp": Loads in a JSON block using JSONP. “jsonp”:使用JSONP加载JSON块。 Adds an extra "?callback=?" 添加额外的“?callback =?” to the end of your URL to specify the callback. 到URL的末尾以指定回调。 Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. 通过将查询字符串参数“_ = [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. 多个空格分隔值:从jQuery 1.5开始,jQuery可以将dataType从Content-Type标头中收到的数据转换为您需要的数据类型。 For example, if you want a text response to be treated as XML, use "text xml" for the dataType. 例如,如果要将文本响应视为XML,请对dataType使用“text xml”。 You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." 您还可以发出JSONP请求,将其作为文本接收,并由jQuery解释为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. 类似地,诸如“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标头,以推断出正确的类型并处理传递给success回调的参数。

Look at the Console tab of your javascript debugging toolbar in the browser. 查看浏览器中javascript调试工具栏的Console选项卡。 It will provide you with more information about the error. 它将为您提供有关错误的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM