Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
AJAX Publicar llamada sin datos

Hola wenn, quiero enviar una solicitud posterior a mi controlador, no hay datos. Traté de registrar mi archivo Json y hay algo. Pero cuando envío la solicitud de publicación, mi controlador muestra que está vacío.

Aquí está mi llamada:

 var item = {}; var jsonObj = []; item["ProductCategoryId"] = i; item["Name"] = txtName; item["Description"] = txtDescription; item["Price"] = txtPrice; item["Stock"] = txtStock; item["ProductCategory"] = txtProductCategory; item["Image"] = await getAsByteArray(txtImage); jsonObj.push(item); var jsonString = JSON.stringify(jsonObj); console.log("jsonString : " + jsonString); $.ajax({ url: "/Admin/SaveProductToDB", type: "POST", data: { dataToSend: jsonString}, success: function (data) { if (data.status == "Success") { BootstrapDialog.show({ title: 'Success!', message: "Data Updated Successfully!", buttons: [{ label: 'OK', action: function (dialog) { window.location.href = "/Admin/Product"; removeProdData(i); $("#btnAddProd").attr("disabled",false); dialog.close(); } }] }); } } }); //Here I make a breakpoint but my string is empty public JsonResult SaveProductToDB(string dataToSend) { List<Product> _List = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Product>>(dataToSend); }

el getAsByteArray

 async function getAsByteArray(file) { return new Uint8Array(await readFile(file)) } function readFile(file) { return new Promise((resolve, reject) => { // Create file reader let reader = new FileReader() // Register event listeners reader.addEventListener("loadend", e => resolve(e.target.result)) reader.addEventListener("error", reject) // Read file reader.readAsArrayBuffer(file) }) }

Descubrí si elimino la Imagen. que el controlador pueda cambiar su tamaño. Gracias por la ayuda hasta ahora. Así que necesito mirar este lugar donde está el problema.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está comprobando data.status como si fuera un hecho que existe. Simplemente console.log(data) en su lugar y verá si se devuelve el estado o no.

Además, si abre la pestaña Red en Chrome, puede hacer clic en la solicitud de publicación y ver si sus encabezados se procesan con precisión y también hacer clic en 'Vista previa' para ver un resultado sin filtrar del controlador.

Es posible que desee modificar su código para detectar errores para la depuración, es decir:

 $.ajax({ url: "/Admin/SaveProductToDB", type: "POST", data: { dataToSend: jsonString}, success: function (data) { if (data.status == "Success") { BootstrapDialog.show({ title: 'Success!', message: "Data Updated Successfully!", buttons: [{ label: 'OK', action: function (dialog) { window.location.href = "/Admin/Product"; removeProdData(i); $("#btnAddProd").attr("disabled",false); dialog.close(); } }] }); } }, error:function (xhr, ajaxOptions, thrownError) { // Set up whatever error reaction you want, here. ie: console.log('An error was encountered.'); alert(xhr.status); alert(thrownError); } });

Otro consejo es validar los datos vacíos que se envían antes de la llamada Ajax, de modo que solo toque el servidor backend cuando sus datos sean válidos, para evitar un error.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!