0

I've got a piece of JavaScript on a webpage that allows a user to scan in serial numbers and then it should send those to a webmethod where a database records it and a txtfile gets made.

It doesn't matter what's in the webmethod, because the code never runs. (Even removing the entire method and just make it change a bool to true as an example returns the same error). Here is the Js:

function sendingReady() {

  saveSerialNumbers();

  var employeeNr = document.getElementById("userCode").textContent;
  var productNr = document.getElementById("productCode").textContent;
  var cnt = {
    employeeNumber: employeeNr,
    productNumber: productNr
  };
  $.ajax({
    type: "POST",
    url: "WebServices.asmx/sendingReady",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify({
      'cnt': cnt
    }),
    success: function() {
      console.log("Success sendingReady")
    },
    error: function() {}
  });
  var succes = "succes";
  localStorage.setItem("SuccesVerzenden", succes);
}

function saveSerialNumbers() {

  var scannedserials = [];
  var errorchoises = [];

  var Table = document.getElementById("SerialListContainer");
  var rowLength = Table.rows.length;

  for (i = 0; i < rowLength; i++) {
    var row = Table.rows[i];

    var serialnumber = row.childNodes[0].textContent;
    scannedserials.push(serialnumber);

    var errorChoice = row.childNodes[1].childNodes[0].value;
    errorchoises.push(errorChoice);
  }

  var cnt = {
    serialNumber: scannedserials,
    sendingErrorcode: errorchoises,
    personalNumber: document.getElementById("userCode").textContent,
    productNumber: document.getElementById("productCode").textContent
  };
  $.ajax({
    type: "POST",
    url: "WebServices.asmx/createSendingCeetisFile",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify({
      'cnt': cnt
    }),
    success: function() {
      console.log("Success saveSerialNumbers")
    },
    error: function() {}
  });

}

To top function works, I get the "success SendingReady" message in my log. But the bottom function, called by the top one, does not. I've tried everything from sending the data as 4 separate instances, to changing the options in the Ajax call, to changing the recipient webmethod. This is said webmethod:

[System.Web.Services.WebMethod]
public void createSendingCeetisFile(SendingErrors cnt)
{
//unimportant code that doesn't ever run anyway; 
}

with sendingErrors being:

public class SendingErrors
{
    public string[] serialNumber { get; set; }
    public string[] sendingErrorcode { get; set; }
    public string[] dollies { get; set; }
    public string personalNumber { get; set; }
    public string productNumber { get; set; }
}

Dollies isn't being used yet. I'm using IIS and I haven't found any way to debug the server-side; All the IIS logs show is a generic '500' with no further detail.

I'm really hoping I'm missing something obvious, but after trawling through these forums and other sites looking for an answer for slightly over a day I'm kind of losing hope.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • `var succes = "succes"; localStorage.setItem("SuccesVerzenden", succes);` is certainly in the wrong place. It needs to be inside the success function - it could be a duplicate of this one: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – mplungjan Mar 27 '17 at 08:37
  • Are you using .Net Framework or .Net Core? – stelioslogothetis Mar 27 '17 at 08:39
  • @mplungjan You're right, that was in the wrong place, but it also wasn't causing the error.. that ajax works just fine, it's the second one (createSendingCeetisFile) that is coming back with an error. – Richard Dain Mar 27 '17 at 09:04
  • @Sty I'm not sure to be honest, I'm rather new to Asp.net, though I have a bigger background with C# I've always just used Visual Studio as it came. I'm not the original author of this code, the people who did assured me it was all very simple and worked, but now I'm stuck debugging stuff only half understand. – Richard Dain Mar 27 '17 at 09:07
  • Easy way to know if you're on .Net Core is to see if your project has a file called `project.json` (that is assuming you aren't using VS 2017 which has eliminated this file). If it doesn't then you're using .Net Framework. – stelioslogothetis Mar 27 '17 at 09:25
  • @Sty Framework then. The only file with project in it's name is project_readme.html – Richard Dain Mar 27 '17 at 09:35
  • Both of serverside methods are within the same webservice or different? Have you uncomment this part `//[System.Web.Script.Services.ScriptService]`? Have you tried debugging the method whether it gets a hit or not? – M. Adeel Khalid Mar 27 '17 at 11:09
  • @MAdeelKhalid They are within the same Webservice, other webmethods do work so this is a given, but yes that part is uncommented. I'm not sure how to debug the serverside code, because when I try to run it via a local debugging setup ('debug -> start debugging' with a breakpoint in the code), it makes those breakpoints white and says they'll never be hit because "no symbols have been loaded for this document". If you have any tips on how to debug a webmethod, those would be helpful. – Richard Dain Mar 27 '17 at 11:32
  • Execute your project by pressing F5, Call your webservice page within the browser, it will show you the methods if declared as public. call the method and see whether it executes successfully or not. – M. Adeel Khalid Mar 27 '17 at 11:36
  • @MAdeelKhalid I've done that, and I can see the webservice/webmethods, but clicking on them just says something about soap. what am i looking for here? this? `HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length` – Richard Dain Mar 27 '17 at 12:39

1 Answers1

0

Thank you M Adeel Khalid and mplungjan for your help. Though none of your responses was the direct answer the understanding it lend me gave me the idea to try and remove the function from within the other function and just call it directly from the button. this did the trick. I'm still getting an odd error, but the function does everything it is supposed to, saving the records to a database and writing it up in a file for use in a different program.

  • So it was most likely a synchro/asynchronicity thing, but I'm still not sure why. That code runs top to bottom is so engrained in my programming background I'm having real problems realizing how this all works. – Richard Dain Mar 27 '17 at 13:08