Reputation: 6783
I have an HTML SPAN (class: displayText) in my MVC view which has "typeId", and "idValue" attributes. The objective is to fetch DisplayText property from a database table for the passed "typeId" and "idValue".
I have number of such SPAN in my view, so I call following method in document.ready() function to get the display text and show it on view, so that users don't need to wait.
$('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;
}
});
})
In above code, UrlGetDisplayName is the ID of HiddenField on my MVC view that contains the URL of following MVC controller action which is responsible to bring display value from database.
[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);
}
This works perfectly in IE and Chrome, but in Firefox it doesn't update the value in SPAN control. I can monitor in Firebug that the Controller action being called and also, it returns correct Json response, but doesn't update the span. What could be the possible reason? And how to fix it?
Upvotes: 0
Views: 237
Reputation: 11984
Try specifying the dataType
of ajax response.Also innerText
will not work with firefox.So try to use html()
or 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);
}
});
Upvotes: 1
Reputation: 64
Please try Text()
or html()
instead of innerText()
.
Like this,
$('span.displayText')[index].Text(result);
Upvotes: 1