我和一些同事遇到了一个问题,即来自 ajax 调用的响应返回了一些意外的内容。 result.responseText 的值不是返回具有各种属性的简单 JSON 对象,而是通用 406 状态错误页面的 HTML 标记,表示浏览器不接受 MIME 类型。
调用是这样进行的:
$.ajax({
url: '/promociones/cincogratis/canjear-codigo-promocional',
type: this.method,
data: $(this).serialize(),
success: function (result) {
$('.promotion_banner .loader').hide();
$('.promotion_banner').html(result);
},
error: function (result) {
var obj = result.responseText;
if (obj.isRedirect) {
document.location = obj.redirectUrl;
}
else {
$('.promotion_banner .loader').hide();
$(".error-wrapper").removeClass("hidden");
var generic_error = document.getElementById('generic_error').value;
$(".error-wrapper p").html(generic_error);
}
},
beforeSend: function() {
$('.promotion_banner .loader').show();
}
});
Controller 对调用的响应如下:
Response.StatusCode = (int)HttpStatusCode.NotAcceptable; // 406
return Json(new { errorMessage = LocalErrorMessages.Website_Promotions_FreeFiver_General_Problem, isRedirect = false } );
我们希望 result.responseText 包含 errorMessage 和 isRedirect 的键值,但它们并不存在。
值得指出的是,这段代码是 Multi-Tenancy 的,由当前应用程序和另一个应用程序共享,在那里它工作得非常好。
我们尝试过:
- 配置 IIS 以显示详细的错误响应,而不是显示更多详细信息的自定义页面 - 对解决问题没有任何额外的帮助。
- 允许对调用的所有响应内容类型
- 改变我们网站的文化(目前是 es-ES)
- 各种 web.config 调整
有没有人遇到过这个问题?
最佳答案
简化您的请求。也许是这样的:
$.ajax({
url: '/promociones/cincogratis/canjear-codigo-promocional',
type: 'GET',
data: {foo:'bar', one:'two'},
dataType: 'json',
success: function (result) {
console.dir(result);
},
error: function (xhr) {
console.dir(xhr)
}
});
并发布来自服务器的响应。这种错误似乎是请求问题而不是服务器配置问题
关于ajax - 返回 JSON 对象时出现 406 错误 – 意外内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16318715/