gpt4 book ai didi

javascript - 使用 WebSocket 使用 AJAX 调用的数据显示和更新表

转载 作者:行者123 更新时间:2023-12-03 04:39:37 25 4
gpt4 key购买 nike

我尝试在 PartialView 中显示和更新表中的数据。我的解决方案不起作用。

这是我的 View 碎片 Index.cshtml

@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>....

部分 View _CurrencyTable.cshtml

@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>

还有 Controller HomeController.cs

        public ActionResult getWebSocketResults(CurrenciesViewModel currencies)
{
if (Request.IsAjaxRequest())
{
return PartialView("_CurrenciesTable", currencies.items);
}
else
{
return View(currencies.items);
}
}
public ActionResult Index()
{
return View();
}

在调试过程中,我在 Controller 和部分 View 中看到来自ajax的数据,但表仍然是空的。

测试屏幕

第一个是debigging部分 View ,第二个是表,第三个是ajax错误函数的错误 - 我不明白这个错误 Debugging PartialView - data is here

And the home page looks like this

This is error from ajax error function

最佳答案

尝试在 ajax 调用中将 dataType: 'json' 替换为 dataType: 'html'

关于javascript - 使用 WebSocket 使用 AJAX 调用的数据显示和更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43142299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com