0

what i'm doing is, i'm calling a C# function so it returns the data i will use in Javascript, However when i read the data from javascript it's always undefined, I debugged the C# function and found out that it actually returns the correct data, so i'm thinking i'm only having trouble receiving it from Javascript.

Here is the c# function i'm calling

  [WebMethod]
    public static string CommonBus(int StopNo1, int StopNo2)
    {
        JavaScriptSerializer oSerializer = new JavaScriptSerializer();
        LinkedList<int> StopBusNo1 = new LinkedList<int>();
        LinkedList<int> StopBusNo2 = new LinkedList<int>();

        StopBusNo1 = LookForBuses(StopNo1); //Returns the buses that stop at StopNo1
        StopBusNo2 = LookForBuses(StopNo2);

       LinkedList<int> CommonBusNos = LookForCommonBuses(StopBusNo1.First, StopBusNo2.First);// Get common buses that stop at both stops 
       LinkedListNode<int> commonNo = CommonBusNos.First;
       LinkedList<Bus> availableBus = new LinkedList<Bus>();

        while (commonNo != null)
        {
            availableBus.AddLast(GetCommonBusIntel(commonNo.Value, StopNo1, StopNo2));
            commonNo = commonNo.Next;
        }

        return oSerializer.Serialize(availableBus);
    }

And here is the Javascipt side

      function FindTransportation(startStops, endStops) {

      for (var i = 0; i < startStops.length; i++) {

          for (var x = 0; x < endStops.length; x++) {

              availabeTransports.push(PageMethods.CommonBus(startStops[i].StopNo, endStops[x].StopNo)); // Trying to push the returned data into an array


          }
      }
  }
6
  • 3
    Maybe I'm missing something, but... Where do you call your server-side WebMethod from your JavaScript client? Commented Dec 2, 2019 at 23:17
  • Yes where is your Javascript Ajax request to your WebMethod CommonBus? Commented Dec 2, 2019 at 23:27
  • Your web method is named CommonBus but your Javascript is calling LookForCommonBuses. Is this intentional? If so, I think some code is missing from your example. Commented Dec 3, 2019 at 1:42
  • my project is in Turkish, i translated it to English so you can understand it better , so names could be wrong here but the actual code calls the method correctly, it calls the method in javascript at the line availabeTransports.push(PageMethods.CommonBus(startStops[i].StopNo, endStops[x].StopNo)); Commented Dec 3, 2019 at 10:15
  • 1
    You can not call like this, You have to use Ajax to call Commented Dec 3, 2019 at 10:24

1 Answer 1

1

Alright found the answer thank you for your comments.

i edited the FindTransportation function

   function FindTransportation(length1, length2) {
      for (var i = 0; i < length1; i++) {
          for (var x = 0; x < length2; x++) {                 
              GetCommonBuses(i, x);              
          }
      }
  }

and i also created the GetCommonBuses function for ajax calls

  function GetCommonBuses(index1,index2) {
      $.ajax({
          type: "POST",
          url: "/HomePage.aspx/CommonBus",
          data: JSON.stringify({ StopNo1: startWalkableStops[index1].StopNo, StopNo2: endWalkableStops[index2].StopNo }),
          contentType: "application/json; charset:utf-8",
          dataType: "json",
      })
          .done(function (res) {
              availabeTransports.push(res);
          });
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.