Javascript 使用WebSocket使用AJAX调用中的数据显示和更新表
我试图在PartialView中显示和更新表中的数据。我的解决方案不起作用 这是我的视图废料索引.cshtml 在调试期间,我在控制器和部分视图中看到来自ajax的数据,但表仍然是空的 从测试中筛选 第一个是debigging部分视图,第二个是表,第三个是来自ajax errof函数的错误-我不理解这个错误Javascript 使用WebSocket使用AJAX调用中的数据显示和更新表,javascript,c#,json,ajax,asp.net-mvc,Javascript,C#,Json,Ajax,Asp.net Mvc,我试图在PartialView中显示和更新表中的数据。我的解决方案不起作用 这是我的视图废料索引.cshtml 在调试期间,我在控制器和部分视图中看到来自ajax的数据,但表仍然是空的 从测试中筛选 第一个是debigging部分视图,第二个是表,第三个是来自ajax errof函数的错误-我不理解这个错误 尝试在ajax调用中将数据类型:“json”替换为数据类型:“html” 它起作用了!非常感谢你! @model IEnumerable<Exchange.WebUI.Models.
尝试在ajax调用中将数据类型:“json”替换为数据类型:“html” 它起作用了!非常感谢你!
@model IEnumerable<Exchange.WebUI.Models.Item>
@{
ViewBag.Title = "Home Page";
}
<script type="text/javascript">
var webSocket;
function webSocketResults() {
webSocket = new WebSocket("ws://....");
webSocket.onmessage = function (event) {
showCurrenciesData(event.data);
$("#webSocketValue").text(event.data);
};
}
function showCurrenciesData(data) {
$.ajax({
type: 'POST',
url: "@Url.Action("getWebSocketResults", "Home")",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: data,
success: function(data) {
$("#currenciesTable").html(data);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(thrownError, xhr, ajaxOptions);
}
});
}
window.onload = webSocketResults;
</script>
<div class="row">
<div class="col-md-6 well well-sm">
<h2>Currencies</h2>
<hr>
@Html.Partial("_CurrenciesTable", Model)
</div>....
@model IEnumerable<Exchange.WebUI.Models.Item>
@{
ViewBag.Title = "_CurrenciesTable";
}
<div id="currenciesTable">
<table class="table table-striped">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.code)
</th>
<th>
@Html.DisplayNameFor(model => model.unit)
</th>
<th>
@Html.DisplayNameFor(model => model.purchasePrice)
</th>
<th>
@Html.DisplayNameFor(model => model.sellPrice)
</th>
<th>
@Html.DisplayNameFor(model => model.averagePrice)
</th>
</tr>
</thead>
@if (Model != null)
{
foreach (var item in Model)
{
<tbody>
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.code)
</td>
<td>
@Html.DisplayFor(modelItem => item.unit)
</td>
<td>
@Html.DisplayFor(modelItem => item.purchasePrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.sellPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.averagePrice)
</td>
</tr>
</tbody>
}
}
else
{
<tbody>
<tr>
<td>
No Connection:
</td>
</tr>
</tbody>
}
</table>
</div>
public ActionResult getWebSocketResults(CurrenciesViewModel currencies)
{
if (Request.IsAjaxRequest())
{
return PartialView("_CurrenciesTable", currencies.items);
}
else
{
return View(currencies.items);
}
}
public ActionResult Index()
{
return View();
}