我有一個api控制器,它回傳一個包含兩個物件串列的物件。
當我使用ajax呼叫這個api時,我期望得到這樣的結果:
一個包含一個長值的物件和兩個物件串列:
我希望當我使用ajax呼叫這個api時,會得到這樣的結果。
{
"userId":26000000000000228,
"paidInvoices":
[{"id":13060000000000039,"total":10},
{"id":13060000000000040,"total":20},
{"id":13060000000000041,"total":30] 。]
"unPaidInvoices":
[{"id":15060000000000039,"total":10}。
{"id":15060000000000040,"total":20}。
{"id":15060000000000041,"total":30}]
}
但我得到了意想不到的重復id(都以40結尾) :
{
"userId":26000000000000228,
"paidInvoices":
[{"id":13060000000000040,"total":10},
{"id":13060000000000040,"total":20},
{"id":13060000000000040,"total":30}] 。
"unPaidInvoices":
[{"id":15060000000000040,"total":10}。
{"id":15060000000000040,"total":20}。
{"id":15060000000000040,"total":30}]
}
代碼:
public class Invoice>
{
public long Id { get; set; }
公共十進制 Total { get; set; }
}
公共 class Invoices { get; set; }
{
public long UserId { get; set; }
public List<Invoice> PaidInvoices{ get; set; }
public List<Invoice> UnPaidInvoices { get; set; }
}
[Route("NetApi/[controller]") ]
[ApiController]
public class PurchasePaymentController : ControllerBase
{
[HttpGet("Search/{id}"/span>)]
public Task<Invoices> Search(long id)
{
var paid = new List<Invoice>
{
new Invoice {Id = 13060000000000039, Total = 10}。
new Invoice {Id = 13060000000000040, Total = 20}。
new Invoice {Id = 13060000000000041, Total = 30}.
};
var unPaid = new List<Invoice>
{
new Invoice {Id = 15060000000000039, Total = 10}。
new Invoice {Id = 150600000000040, Total = 20}。
new Invoice {Id = 15060000000000041, Total = 30}.
};
return Task. FromResult(new Invoices {UserId = id, PaidInvoices = paid, UnPaidInvoices = unPaid})。)
}
}
我試著回傳JsonResult,但這并沒有什么不同。 我從這個沒有使用任何布局的razorPage中呼叫它:
@page
@model MyWebSite.Pages.Test.IndexModel。
@{
Layout = null;
}
<html>
<head> /span>
<script src="https://ajax. googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>/span>
</head>
<body>
<h1>Welcome</h1>/span>
<div id=" result"> </div>>
<script>
//如果我使用$.ajax或$.get,結果是一樣的(奇怪/意外的結果)
$(function () {
$.ajax({
type: "GET"。
url: "@Url. RouteUrl("NetApi", new {controller = "PurchasePayment" 。action = "Search", id = 26000000000000228}"。
contentType: "application/json; charset=utf-8"。
dataType: "json",
success: getCallback
});
});
$(function() {
debugger;
$.get("@Url. RouteUrl("NetApi", new {controller = "PurchasePayment" 。action = "Search", id = 26000000000000228}",
getCallback, "json")。)
});
function getCallback(response) {
console.log(JSON.stringify( response))。
debugger。
}
//but this load method works fine.
$(function() {
$("#result").load("@Url。 RouteUrl("NetApi", new {controller = "PurchasePayment" 。action = "Search", id = 26000000000000228})")。
});
</script>>
</body>/span>
</html>
在我的Startup.cs中
// ConfigureServices
services.AddMvc().AddNewtonsoftJson(options =>/span>
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore>.
);
// Configure
app.UseStaticFiles()。
app.UseRouting()。
app.UseAuthentication()。
app.UseAuthorization()。
app.UseEndpoints(endpoints =>)
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}")。)
endpoints.MapControllerRoute(
name: "api"。
pattern: "api/{controller}/{action}/{id?}"。
defaults: new { controller = "login", action = "Index" }) 。
endpoints.MapControllerRoute(
name: "NetApi"。
pattern: "NetApi/{controller}/{action}/{id?}")。)
endpoints.MapRazorPages()。
});
uj5u.com熱心網友回復:
我使用Postman測驗了你的Api,一切作業正常。所以問題出在Javascript上。如果你在你的Id長數字中洗掉一個零,在javascript中也能正常作業。
數值15060000000000039實際上并沒有超過JavaScript中的最大數值,但是,這個數值超過了 "積分精度 "的范圍,有時也被稱為整數精度的大小,即2 pow 53。 這并不是說發送了錯誤的數字:而是說15060000000000039和13060000000000041的字面意思只能精確表示為13060000000000040,因此在JavaScript中從來沒有正確的數值。(積分的范圍大約是 /-253。) 問題在于javascript對大整數的取舍方式。
因此,如果你仍然要使用javascript,我可以提供的變通方法是將這個長數字作為一個字串發送。如果你需要對這些數字進行操作,有很多javascript BigInt庫。但你似乎不需要它,因為它們只是發票上的數字。所以把你的類改成這樣
public class Invoice
{
public string Id { get; set; }
公共十進制 Total { get; set; }
}
和資料
var paid = new List<Invoice>
{
new Invoice {Id = "13060000000000039" /span>, Total = 10}。
new Invoice {Id = "13060000000000040" , Total = 20}。
....,以此類推
或者使用這個,那么你就不需要改變任何東西了
public classInvoice
{
[JsonIgnore]
public long Id { get; set; }
public string InvoiceId { get { return Id.ToString(); } }
public decimal Total { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321681.html
標籤:
