[英]Post request error in Api Fetch with asp.net C #
I am making a post request with api fetch, at the moment I reach the breakpoint the parameters arrive or inform me that they are null, therefore this part generates an error, I have already verified the variables to which it assigns the html objects, the variables you sent are user identification, start date and end date, this data was sent to an object that requires the C # method, so it makes a query with the information that I require, which I want to know why when doing this method it arrives null data to the backend, annex code and running image我正在使用 api 获取发布请求,在我到达断点的那一刻,参数到达或通知我它们是 null,因此这部分会产生错误,我已经验证了它分配给 html 对象的变量,您发送的变量是用户标识、开始日期和结束日期,此数据已发送到需要 C # 方法的 object,因此它使用我需要的信息进行查询,我想知道为什么在执行此方法时到达null数据到后台,附件代码和运行图片
View看法
@using System.Globalization
@using Microsoft.AspNetCore.Http.Features
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = "Reporte Rh";
}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Main content -->
<section class="content">
<!-- Default box -->
<div class="box">
<div class="box-header with-border">
<h3 class="box-title text-primary"><i class="fa fa-building-o"></i> @ViewData["Title"]</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip"
title="Collapse">
<i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
<i class="fa fa-times"></i>
</button>
</div>
</div>
<div class="box-body">
<div id="Grid">
<form role="form" method="post">
<div class="form-group has-success">
<label class="control-label" for="inputSuccess"><i class="fa fa-check"></i>@Localizer["Cedula"]</label>
<input type="text" class="form-control" id="cedula" placeholder="Enter ...">
</div>
<label for="start" class="control-label">Start date:</label>
<input type="date" class="form-control" id="start" name="trip-start"
value="2018-07-22"
min="2018-01-01" max="3000-12-31">
<label for="start" class="control-label">And date:</label>
<input type="date" class="form-control" id="end" name="trip-start"
value="2018-07-22"
min="2018-01-01" max="3000-12-31">
<!-- /.col Botom-->
<div class="col-xs-4">
<button type="button" class="btn btn-primary btn-sm" onclick="ValidarExisteContacto()">@Localizer["Reporte"]</button>
</div>
</form>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
</div>
<!-- /.box-footer-->
</div>
<!-- /.box -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<script src="~/Scripts/ReporteRh/ValidacionReporteReporteRh.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
function ValidarExisteContacto() {
var existeUsuario;
if ($("#cedula").val() !== null || $("#start").val() !== null || $("#end").val() !== null) {
//const url = '/ReporteRh/CreatePdfs';
var url = '/ReporteRh/CreatePdfs';
var cedula = $("#cedula").val();
var start = $("#start").val();
var end = $("#end").val();
const myPost = {
cedula: cedula,
start: start,
end: end
};
fetch(url, {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(myPost)
}).then((res) => {
if (res.ok) {
existeUsuario = true;
console.log(res);
return res.json();
} else {
existeUsuario = false;
console.log(res);
return Promise.reject({ status: res.status, statusText: res.statusText });
}
})
.then((data) => console.log(data))
.catch(err => console.log('Error message:', err.statusText) );
}
return existeUsuario;
}
/// <summary>
/// Metodo para el reporte
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public FileResult CreatePdfs(ReporteLabViewModels model)
{
//ReporteLabViewModels model = new ReporteLabViewModels();
MemoryStream workStream = new MemoryStream();
StringBuilder status = new StringBuilder("");
DateTime dTime = DateTime.Now;
//Crea archivo con nombre
string strPDFFileName = string.Format("SamplePdf" + dTime.ToString("yyyyMMdd") + "-" + ".pdf");
if (model.cedula != null)
{
var sql = from d in demoDrHelpContext.DatosPacientes
join rh in demoDrHelpContext.Rh on d.Id equals rh.IdPaciente
join gr in demoDrHelpContext.GrupoSanguineo on rh.IdGrupoSanguineo equals gr.Id
join nt in demoDrHelpContext.NotificacionExamen on d.Id equals nt.IdPaciente
where nt.FechaRegistro >= model.start && nt.FechaRegistro <= model.end && d.Codigo == model.cedula
select new { d.Nombre, d.Codigo, d.Edad, gr.Sigla, nt.FechaRealizada };
if (sql == null)
{
foreach (var grouprh in sql)
{
#region Area del reporte pdf
iTextSharp.text.Document doc = new iTextSharp.text.Document();
doc.SetMargins(0f, 0f, 0f, 0f);
//Crea PDF Table con 5 columnas
PdfPTable tableLayout = new PdfPTable(5);
doc.SetMargins(0f, 0f, 0f, 0f);
//Create PDF Table
PdfWriter.GetInstance(doc, workStream).CloseStream = false;
doc.Open();
// Crear un ImageData object
//Agrega la tabla del pdf
string imageURL = "C:/Apoyo Mederic/archive/APOYO MEDERI-1.png";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
iTextSharp.text.Image jpgs = iTextSharp.text.Image.GetInstance(imageURL);
//Cambiar el tamaño de la imagen depende de su necesidad
jpg.ScaleToFit(120f, 100f);
//Dar espacio antes de la imagen
jpg.SpacingBefore = 10f;
//Dar algo de espacio después de la imagen.
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.ALIGN_CENTER;
// Escribimos el encabezamiento en el documento
Font fonts = new Font(FontFamily.HELVETICA, 8, Font.BOLD);
doc.Add(new Paragraph(dTime.ToString("dd/MM/yyyy"), fonts));
doc.Add(Chunk.NEWLINE);
// Este codigo genera una tabla de 3 columnas
PdfPTable tablets = new PdfPTable(2);
tablets.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
Font font = new Font(FontFamily.HELVETICA, 16, Font.BOLD);
Paragraph pLong = new Paragraph("LABORATORIO CLINICO", font);
pLong.Alignment = Element.ALIGN_CENTER;
doc.Add(pLong);
// Creando una tabla
float[] pointColumnWidths = { 150F, 150F };
PdfPTable tablew = new PdfPTable(pointColumnWidths);
// Adding cells to the table
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Adding cells to the table
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(jpg);
// Agregar celdas a la tabla
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Adding cells to the table
tablew.AddCell(new PdfPCell(new Phrase("Numero Documeno ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase("Nombre ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase("Fecha ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(grouprh.FechaRealizada.ToString(), new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
//tablew.AddCell(new PdfPCell(new Phrase("Activo ", new Font(Font.FontFamily.HELVETICA, 14, 1,
// new iTextSharp.text.BaseColor(0, 0, 0))))
//{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
//tablew.AddCell(new PdfPCell(new Phrase(genero.Activo.ToString(), new Font(Font.FontFamily.HELVETICA, 14, 1,
// new iTextSharp.text.BaseColor(0, 0, 0))))
//{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Agregar celdas a la tabla
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Agregar celdas a la tabla
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Agregar tabla al documento
doc.Add(tablew);
doc.Add(tablets);
doc.Add(Add_Content_To_PDFS(tableLayout, model));
// Cierra el documento
doc.Close();
#endregion
}
}
else
{
foreach (var grouprh in sql)
{
#region Area del reporte pdf
iTextSharp.text.Document doc = new iTextSharp.text.Document();
doc.SetMargins(0f, 0f, 0f, 0f);
//Crea PDF Table con 5 columnas
PdfPTable tableLayout = new PdfPTable(5);
doc.SetMargins(0f, 0f, 0f, 0f);
//Create PDF Table
PdfWriter.GetInstance(doc, workStream).CloseStream = false;
doc.Open();
// Crear un ImageData object
//Agrega la tabla del pdf
string imageURL = "C:/Apoyo Mederic/archive/APOYO MEDERI-1.png";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
iTextSharp.text.Image jpgs = iTextSharp.text.Image.GetInstance(imageURL);
//Cambiar el tamaño de la imagen depende de su necesidad
jpg.ScaleToFit(120f, 100f);
//Dar espacio antes de la imagen
jpg.SpacingBefore = 10f;
//Dar algo de espacio después de la imagen.
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.ALIGN_CENTER;
// Escribimos el encabezamiento en el documento
Font fonts = new Font(FontFamily.HELVETICA, 8, Font.BOLD);
doc.Add(new Paragraph(dTime.ToString("dd/MM/yyyy"), fonts));
doc.Add(Chunk.NEWLINE);
// Este codigo genera una tabla de 3 columnas
PdfPTable tablets = new PdfPTable(2);
tablets.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
Font font = new Font(FontFamily.HELVETICA, 16, Font.BOLD);
Paragraph pLong = new Paragraph("LABORATORIO CLINICO", font);
pLong.Alignment = Element.ALIGN_CENTER;
doc.Add(pLong);
// Creando una tabla
float[] pointColumnWidths = { 150F, 150F };
PdfPTable tablew = new PdfPTable(pointColumnWidths);
// Adding cells to the table
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Adding cells to the table
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(jpg);
// Agregar celdas a la tabla
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Adding cells to the table
tablew.AddCell(new PdfPCell(new Phrase("Codigo ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(grouprh.Codigo.ToString(), new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase("Nombre ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(grouprh.Nombre.ToString(), new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase("Activo ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
tablew.AddCell(new PdfPCell(new Phrase(grouprh.FechaRealizada.ToString(), new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Agregar celdas a la tabla
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Agregar celdas a la tabla
tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT }); tablew.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 14, 1,
new iTextSharp.text.BaseColor(0, 0, 0))))
{ Border = 0, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_RIGHT });
// Agregar tabla al documento
doc.Add(tablew);
doc.Add(tablets);
GrupoSanguineoViewModels rhViewmModels = new GrupoSanguineoViewModels
{
Sigla = grouprh.Sigla
};
doc.Add(Add_Content_To_PDF(tableLayout, rhViewmModels));
// Cierra el documento
doc.Close();
#endregion
}
}
}
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return File(workStream, "application/pdf", strPDFFileName);
}
Demonstration from the browser side浏览器端的演示
I was able to solve the problem the object could link the name of the objects of the text我能够解决 object 可以链接文本对象名称的问题
function ValidarExisteContacto() {
var existeUsuario;
if ($("#cedula").val() !== null || $("#start").val() !== null || $("#end").val() !== null) {
const myPost = {
cedula: $("#cedula").val(),
start: $("#start").val(),
end: $("#end").val()
};
fetch(url, {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(myPost)
}).then((res) => {
if (res.ok) {
existeUsuario = true;
console.log(res);
return res.json();
} else {
existeUsuario = false;
console.log(res);
return Promise.reject({ status: res.status, statusText: res.statusText });
}
})
.then((data) => console.log(data))
.catch(err => console.log('Error message:', err.statusText) );
}
return existeUsuario;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.