【问题标题】:Json response not getting updated in FirefoxJson 响应未在 Firefox 中更新
【发布时间】:2013-08-01 13:51:45
【问题描述】:

我的 MVC 视图中有一个 HTML SPAN(类:displayText),它具有“typeId”和“idValue”属性。 目标是从传递的“typeId”和“idValue”的数据库表中获取 DisplayText 属性。

我的视图中有多个这样的 SPAN,所以我在 document.ready() 函数中调用以下方法来获取显示文本并将其显示在视图中,这样用户就不需要等待了。

$('span.displayText').each(
function (index) {
    var url = $('#UrlGetDisplayName').val();
    $.ajax({
        type: "GET",
        url: url,
        data: ({ typeId: $(this).attr('typeId'),
            value: $(this).attr('idValue')
        }),
        success: function (result) {
            $('span.displayText')[index].innerText = result;
        }
    });
})

在上面的代码中,UrlGetDisplayName 是我的 MVC 视图上 HiddenField 的 ID,它包含以下 MVC 控制器操作的 URL,该操作负责从数据库中获取显示值。

[HttpGet]
public virtual JsonResult GetDisplayTextByType(string typeId, string idValue)
{
    string displayText = "";
    /* Code to call a database method to fetch text by type, and assign it to displayText */
return Json(displayText, JsonRequestBehavior.AllowGet);
}

这在 IE 和 Chrome 中完美运行,但在 Firefox 中它不会更新 SPAN 控件中的值。我可以在 Firebug 中监视正在调用的 Controller 操作,并且它返回正确的 Json 响应,但不更新跨度。可能的原因是什么?以及如何解决?

【问题讨论】:

标签: asp.net-mvc json jquery


【解决方案1】:

请尝试Text()html() 而不是innerText()

这样,

$('span.displayText')[index].Text(result);

【讨论】:

  • 酷,我意识到这是 Firefox 不支持的 innerText 属性的问题。我在stackoverflow.com/questions/1359469/… 找到了另一种解决方案。检查bobnice的答案。感谢您的帮助,这帮助我确定了问题所在的区域,因此将其标记为答案。 :)
【解决方案2】:

尝试指定ajax响应的dataType。另外innerText不适用于firefox。所以尝试使用html()text()

 $.ajax({
        type: "GET",
        url: url, 
        dataType:'json',
        data: ({ typeId: $(this).attr('typeId'),
            value: $(this).attr('idValue')
        }),
        success: function (result) {
            var response    = JSON.stringify(result);
            response        = JSON.parse(response);
            console.log(response);//Logging the response to browser console
            $('span.displayText')[index].html(response);
        }
    });

【讨论】:

    猜你喜欢
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2013-06-02
    相关资源
    最近更新 更多