0

First of all, what i am trying to do is to populate a drop down box like this:

<asp:DropDownList CssClass="ui dropdown" ID="drpGolfClub" runat="server"></asp:DropDownList>

With some values giving me golf club id and names. The problem is that with jQuery i cant seem to run the code in the vb file behind. it all looks like this. In the aspx file:

 function loadGolfClub()
        {
                alert('inside method');
                //debugger;
                $.ajax({
                    type: "POST",
                    url: "Register.aspx/LoadGolfClub",
                    data: '{}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function (Result) {

                        debugger;
                        Result = Result.d;
                        $('#<%=drpGolfClub.ClientID%>').empty();
                        $('#<%=drpGolfClub.ClientID%>').prepend("<option value='0'>" +"Select" + "</option>");
                        $.each(Result, function (key, value) {
                            $('#<%=drpGolfClub.ClientID%>').append($("<option></option>").val(value.GolfClubId).html(value.GolfClubName));
                            $('#<%=drpGolfClub.ClientID%>')[0].selectedIndex = 1;
                        });
                        alert("in sucess!");
                    },

                    failure: function () {
                        alert("Failed!");
                    }
                });
            } 

The aspx file's vb behind is added like this:

Imports System
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports Microsoft.AspNet.Identity.Owin
Imports Owin
Imports System.Data
Imports System.Web.Services
Imports System.Reflection
Imports Golfbook.CoreLibrary
Imports Golfbook.CoreLibrary.Golfbook.Services


Public Class Register
    Inherits System.Web.UI.Page
    Shared objGCBO As New GolfClubBO()

    Shared objGolfClubService As New Golfbook.CoreLibrary.Golfbook.Services.GolfClub.GolfClubDetails

    Shared details As New List(Of GolfClubBO)()
    Shared _loginName As String
    Shared loginName As String

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub

    <WebMethod()>
        <System.Web.Script.Services.ScriptMethod(ResponseFormat:=System.Web.Script.Services.ResponseFormat.Json)>
        Public Shared Function LoadGolfClub() As GolfClubBO()
            Dim details As New List(Of GolfClubBO)()
            Try
                details = objGolfClubService.LoadGolfClub()
            Catch ex As Exception

            End Try
            Return details.ToList.ToArray()
        End Function
End Class

I know that page_load in vb is run, but it never goes to the function there. I have tried "everything" now, and have no idea what to do. the alert boxes added in the function is trigging, so it do begin to run

I know the JQuery is working cause i have added som values into a random textbox: $('#txtFirstName').val('Martin');

Any tips on what can be the reason of this?

Agnel Amodia
  • 765
  • 8
  • 18
Martin
  • 39
  • 7
  • 1) is `loadGolfClub()` executed somewhere in your page? 2) If so, have you checked your browser's network tools to know if the ajax request executes and what the response is? 3) `Catch ex As Exception End Try` is a dreadful anti-patten - don't suppress unexpected exceptions, because you'll never know why your code fails. Let the app fail gracefully, and also log all exceptions (at a global level) for future investigation 4) `async: false` is also a bad idea, it's unnecessary, locks the browser UI up and is deprecated anyway. You don't need or want it. – ADyson Mar 06 '18 at 13:05
  • 1) Yes it is loaded on $(document).ready(function (){}. 2) i really dont know how to check that, but i cant see any error messages in console at least.3) I will add en error message there, but it shouldnt have anything to do with this, since the function never is started at all.4) i have removed the async:false, but that didnt help either. – Martin Mar 06 '18 at 13:53
  • "i really dont know how to check that,"...open your browser's Developer tools and go to the Network section. Then refresh your page and watch to see if the ajax call to LoadGolfClub occurs or not. If it does, you can click on it to open more details including the request details and response data. You want to check the response code ("200 OK" indicates it ran successfully without crashing), and also the Response body itself. – ADyson Mar 06 '18 at 14:56
  • Is this a message you have seen before? {Message: "Godkjenning mislyktes.", StackTrace: null,…} ExceptionType : "System.InvalidOperationException" Message : "Godkjenning mislyktes." StackTrace : null – Martin Mar 06 '18 at 15:03
  • "Godkjenning mislyktes" is "Authentication failed", right? Norwegian? Anyway I guess it means the server thinks you're not logged in. Are you required to supply a session cookie or other kind of authentication token when making requests to your site? Also what was the HTTP code returned? 401? You can google that error by the way, it returns some interesting things including https://stackoverflow.com/questions/23033614/asp-net-calling-webmethod-with-jquery-ajax-401-unauthorized – ADyson Mar 06 '18 at 15:10
  • You are absolutely right! I read your link and the issue was exactly that! Thank you very much for guiding me in the right way! :) – Martin Mar 07 '18 at 08:12

1 Answers1

1

As written above, thanks to ADyson, the answer to my trouble was fixing this: Inside ~/App_Start/RouteConfig.cs change:

settings.AutoRedirectMode = RedirectMode.Permanent;
To:

settings.AutoRedirectMode = RedirectMode.Off;

Thank you all for the response!

Martin
  • 39
  • 7