0

I am new to using jquery and have an issue with the using it. I have the following included in the html:

    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.4.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.2.4.min.js"></script>
    <script src="resources/jquery/jquery.validate.js"></script>
    <script src="resources/jquery/jquery.validate.min.js"></script>
    <script src="resources/jquery/additional-methods.js"></script>
    <script src="resources/jquery/additional-methods.min.js"></script>
    <link href="resources/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" />
    <link href="resources/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css" rel="stylesheet" />
    <script src="resources/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

When I submit the post:

            submitHandler : function(contactForm) {
            //do something here
            //var frm = $('#contactForm');
            //alert($("#accountName").val());

            $.ajax({
                type: "POST",
                url: "LoginView",
                cache: false,
                data: $(contactForm).serialize(),
                success: function(data){
                    if(data == "1"){
                        window.location.assign("SelectPerson.html");
                    }else{
                        $('#ajaxGetUserServletResponse').text(data);
                        $("#accountName").focus();
                    }
                    console.log('Submission was successful.');
                    console.log(data);
                }
            });

I inspect the Chrome console at the following is displayed:

jquery-2.2.4.min.js:4 POST http://localhost:8080/AwardTracker_NJE/LoginView 404 ()
send    @   jquery-2.2.4.min.js:4
ajax    @   jquery-2.2.4.min.js:4
submitHandler   @   login-ajax.js:31
d   @   jquery.validate.min.js:4
(anonymous) @   jquery.validate.min.js:4
dispatch    @   jquery-2.2.4.min.js:3
r.handle    @   jquery-2.2.4.min.js:3

In the Source I have jquery-2.2.4.min.js the the following is underlined in red:

try{h.send(b.hasContent&&b.data||null)}catch(i){if(c)throw i}},abort:function(){c&&c()}}:void 0}),n.ajaxSetup({accepts:{script:"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"},contents:{script:/\b(?:java|ecma)script\b/},converters:{"text script":function(a){return n.globalEval(a),a}}}),n.ajaxPrefilter("script",function(a){void 0===a.cache&&(a.cache=!1),a.crossDomain&&(a.type="GET")}),n.ajaxTransport("script",function(a){if(a.crossDomain){var b,c;return{send:function(e,f){b=n("<script>").prop({charset:a.scriptCharset,src:a.url}).on("load error",c=function(a){b.remove(),c=null,a&&f("error"===a.type?404:200,a.type)}),d.head.appendChild(b[0])},abort:function(){c&&c()}}}});var Jb=[],Kb=/(=)\?(?=&|$)|\?\?/;n.ajaxSetup({jsonp:"callback",jsonpCallback:function(){var a=Jb.pop()||n.expando+"_"+kb++;return this[a]=!0,a}}),n.ajaxPrefilter("json jsonp",function(b,c,d){var e,f,g,h=b.jsonp!==!1&&(Kb.test(b.url)?"url":"string"==typeof b.data&&0===(b.contentType||"").indexOf("application/x-www-form-urlencoded")&&Kb.test(b.data)&&"data");return h||"jsonp"===b.dataTypes[0]?(e=b.jsonpCallback=n.isFunction(b.jsonpCallback)?b.jsonpCallback():b.jsonpCallback,h?b[h]=b[h].replace(Kb,"$1"+e):b.jsonp!==!1&&(b.url+=(lb.test(b.url)?"&":"?")+b.jsonp+"="+e),b.converters["script json"]=function(){return g||n.error(e+" was not called"),g[0]},b.dataTypes[0]="json",f=a[e],a[e]=function(){g=arguments},d.always(function(){void 0===f?n(a).removeProp(e):a[e]=f,b[e]&&(b.jsonpCallback=c.jsonpCallback,Jb.push(e)),g&&n.isFunction(f)&&f(g[0]),g=f=void 0}),"script"):void 0}),n.parseHTML=function(a,b,c){if(!a||"string"!=typeof a)return null;"boolean"==typeof b&&(c=b,b=!1),b=b||d;var e=x.exec(a),f=!c&&[];return e?[b.createElement(e[1])]:(e=ca([a],b,f),f&&f.length&&n(f).remove(),n.merge([],e.childNodes))};var Lb=n.fn.load;n.fn.load=function(a,b,c){if("string"!=typeof a&&Lb)return Lb.apply(this,arguments);var d,e,f,g=this,h=a.indexOf(" ");return h>-1&&(d=n.trim(a.slice(h)),a=a.slice(0,h)),n.isFunction(b)?(c=b,b=void 0):b&&"object"==typeof b&&(e="POST"),g.length>0&&n.ajax({url:a,type:e||"GET",dataType:"html",data:b}).done(function(a){f=arguments,g.html(d?n("<div>").append(n.parseHTML(a)).find(d):a)}).always(c&&function(a,b){g.each(function(){c.apply(this,f||[a.responseText,b,a])})}),this},n.each(["ajaxStart","ajaxStop","ajaxComplete","ajaxError","ajaxSuccess","ajaxSend"],function(a,b){n.fn[b]=function(a){return this.on(b,a)}}),n.expr.filters.animated=function(a){return n.grep(n.timers,function(b){return a===b.elem}).length};function Mb(a){return n.isWindow(a)?a:9===a.nodeType&&a.defaultView}n.offset={setOffset:function(a,b,c){var d,e,f,g,h,i,j,k=n.css(a,"position"),l=n(a),m={};"static"===k&&(a.style.position="relative"),h=l.offset(),f=n.css(a,"top"),i=n.css(a,"left"),j=("absolute"===k||"fixed"===k)&&(f+i).indexOf("auto")>-1,j?(d=l.position(),g=d.top,e=d.left):(g=parseFloat(f)||0,e=parseFloat(i)||0),n.isFunction(b)&&(b=b.call(a,c,n.extend({},h))),null!=b.top&&(m.top=b.top-h.top+g),null!=b.left&&(m.left=b.left-h.left+e),"using"in b?b.using.call(a,m):l.css(m)}},n.fn.extend({offset:function(a){if(arguments.length)return void 0===a?this:this.each(function(b){n.offset.setOffset(this,a,b)});var b,c,d=this[0],e={top:0,left:0},f=d&&d.ownerDocument;if(f)return b=f.documentElement,n.contains(b,d)?(e=d.getBoundingClientRect(),c=Mb(f),{top:e.top+c.pageYOffset-b.clientTop,left:e.left+c.pageXOffset-b.clientLeft}):e},position:function(){if(this[0]){var a,b,c=this[0],d={top:0,left:0};return"fixed"===n.css(c,"position")?b=c.getBoundingClientRect():(a=this.offsetParent(),b=this.offset(),n.nodeName(a[0],"html")||(d=a.offset()),d.top+=n.css(a[0],"borderTopWidth",!0),d.left+=n.css(a[0],"borderLeftWidth",!0)),{top:b.top-d.top-n.css(c,"marginTop",!0),left:b.left-d.left-n.css(c,"marginLeft",!0)}}},offsetParent:function(){return this.map(function(){var a=this.offsetParent;while(a&&"static"===n.css(a,"position"))a=a.offsetParent;return a||Ea})}}),n.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(a,b){var c="pageYOffset"===b;n.fn[a]=function(d){return K(this,function(a,d,e){var f=Mb(a);return void 0===e?f?f[b]:a[d]:void(f?f.scrollTo(c?f.pageXOffset:e,c?e:f.pageYOffset):a[d]=e)},a,d,arguments.length)}}),n.each(["top","left"],function(a,b){n.cssHooks[b]=Ga(l.pixelPosition,function(a,c){return c?(c=Fa(a,b),Ba.test(c)?n(a).position()[b]+"px":c):void 0})}),n.each({Height:"height",Width:"width"},function(a,b){n.each({padding:"inner"+a,content:b,"":"outer"+a},function(c,d){n.fn[d]=function(d,e){var f=arguments.length&&(c||"boolean"!=typeof d),g=c||(d===!0||e===!0?"margin":"border");return K(this,function(b,c,d){var e;return n.isWindow(b)?b.document.documentElement["client"+a]:9===b.nodeType?(e=b.documentElement,Math.max(b.body["scroll"+a],e["scroll"+a],b.body["offset"+a],e["offset"+a],e["client"+a])):void 0===d?n.css(b,c,g):n.style(b,c,d,g)},b,f?d:void 0,f,null)}})}),n.fn.extend({bind:function(a,b,c){return this.on(a,null,b,c)},unbind:function(a,b){return this.off(a,null,b)},delegate:function(a,b,c,d){return this.on(b,a,c,d)},undelegate:function(a,b,c){return 1===arguments.length?this.off(a,"**"):this.off(b,a||"**",c)},size:function(){return this.length}}),n.fn.andSelf=n.fn.addBack,"function"==typeof define&&define.amd&&define("jquery",[],function(){return n});var Nb=a.jQuery,Ob=a.$;return n.noConflict=function(b){return a.$===n&&(a.$=Ob),b&&a.jQuery===n&&(a.jQuery=Nb),n},b||(a.jQuery=a.$=n),n});

With the error message "Failed to load resource: the server responded with a status of 404 ()"

​The network tab is:

enter image description here

The folder structure is:

enter image description here

enter image description here

My LoginView.java WebServlet is:

package client;    
/**
 * The purpose of this View is to allow the account holder
 * to login to their account using an Account Name and password.
 */

import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import server.MySQLConnection;
import shared.AccountGroup;
import shared.AccountCubAssociation;


@WebServlet("/LoginView")
public class LoginView extends HttpServlet implements Serializable {

    private static final long serialVersionUID = 1L;

    int loginAttempt = 0;
    int youthMembersLinked = 0;
    public String youthMemberID = null;
    public String nextView = "Next View";

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String textBoxAccountName = request.getParameter("accountName"); // From bootstrap
        String passwordTextBox = request.getParameter("enterPassword"); // From bootstrap
        System.out.println("In Java.");

        // If the required details have been entered then search the database for a
        // match            
        AccountGroup accountGroup = MySQLConnection.authenticateAccountGroup(textBoxAccountName, passwordTextBox);

        //Validate the Account
        if (accountGroup != null) {
            //Authenticated
            System.out.println("Authenicated");
            if (accountGroup.getLevel() == null) {
                response.getWriter().write("Your account has not yet been set up. Please contact your administrator.");
            }else{
                if (accountGroup.getArchived() != null) {
                    response.getWriter().write("Your account has been archived. Please contact your administrator.");
                }else{
                    if (accountGroup.getEnabled() == 0) {
                        response.getWriter().write("Your account has been disabled. Please contact your administrator.");
                    }else{
                        //Record login details
                        LocalDateTime date = LocalDateTime.now();
                        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                        String nowDateTime = date.format(dtf);

                        MySQLConnection.addUse(accountGroup.getAccountId(), nowDateTime, accountGroup.getLevel(), accountGroup.getGroupCountry(), accountGroup.getGroupState(),
                            accountGroup.getGroupRegion(), accountGroup.getGroupDistrict(), accountGroup.getGroupName(),
                            accountGroup.getGroupSection(), accountGroup.getGroupSubSection());                     

                        //Determine which view to display next
                        if (accountGroup.getLevel().equals("Youth Member")) {
                            //Get the Youth Member's ID and store it for the next view.
                            AccountCubAssociation accountCubAssociation = MySQLConnection.getAccountCubsAssociation(accountGroup.getAccountId());

                            //Determine if the account is linked to a Youth Member
                            if (accountCubAssociation == null) {
                                youthMembersLinked = 0;
                                response.getWriter().write("Your account is not linked. Please contact the Administrator.");
                            }else{
                                //Store account information for securely passing between views
                                MySQLConnection.setViewData(accountGroup.getAccountId(), accountGroup.getLevel(), accountCubAssociation.getCdId(), null, null, accountGroup.getScoutGroupId(),
                                        null, 0, accountGroup.getGroupSection(), null, null, null, null, accountGroup.getGroupNAS());

                                //Go to next view
                                if (accountGroup.getGroupSection().equals("Joey") || accountGroup.getGroupSection() == "Joey") {
                                    //Go to next view - JoeyAwardOverview.html
                                    response.getWriter().write("13");
                                }else{
                                    if (accountGroup.getGroupSection().equals("Cub") || accountGroup.getGroupSection() == "Cub") {
                                        //Go to next view - AwardOverview.html
                                        response.getWriter().write("14");
                                    }else{
                                        if (accountGroup.getGroupSection().equals("Scout") || accountGroup.getGroupSection() == "Scout") {
                                            //Go to next view - ScoutAwardOverview.html
                                            response.getWriter().write("15");
                                        }
                                        if (accountGroup.getGroupSection().equals("Venturer") || accountGroup.getGroupSection() == "Venturer") {
                                            //Go to next view - VenturerAwardOverview.html
                                            response.getWriter().write("16");
                                        }
                                        if (accountGroup.getGroupSection().equals("Rover") || accountGroup.getGroupSection() == "Rover") {
                                            //Go to next view - RoverAwardOverview.html
                                            response.getWriter().write("17");
                                        }
                                    }
                                }
                            }
                        } else {
                            if (accountGroup.getLevel().equals("Non-leader")) {
                                //Determine if the account is linked to one Youth Member
                                //If linked to one Youth Member then go to the view to display
                                //If linked to more than one Youth Member then go to the view
                                //to allow a Youth Member to be selected.
                                List<AccountCubAssociation> accountCubAssociationList = MySQLConnection.listAccountCubsAssociation(accountGroup.getAccountId());

                                if (accountCubAssociationList.isEmpty()) {
                                    youthMembersLinked = 0;
                                }else{
                                    for (final AccountCubAssociation accountCubAssociation : accountCubAssociationList) {
                                        //If the account is linked to a Youth Member then record
                                        //the Youth Member ID and count how many as we want to go
                                        //directly to AccountOverviewView if there is only one
                                        //Youth Member and pass the Youth Member ID.
                                        youthMemberID = accountCubAssociation.getCdId();
                                        youthMembersLinked++;
                                    }
                                }

                                if (youthMembersLinked == 0) {
                                    response.getWriter().write("You do not have any Youth Members to view. Please contact the Administrator.");
                                }else{
                                    if (youthMembersLinked == 1) {
                                        //Is a Non-leader with one Youth Member.
                                        //Store the LoginView data for use in subsequent Views.
                                        MySQLConnection.setViewData(accountGroup.getAccountId(), accountGroup.getLevel(), youthMemberID, null, null, accountGroup.getScoutGroupId(),
                                                null, 0, accountGroup.getGroupSection(), null, null, null, null, accountGroup.getGroupNAS());

                                        //Go to next view
                                        if (accountGroup.getGroupSection().equals("Joey") || accountGroup.getGroupSection() == "Joey") {
                                            //Go to next view - JoeyAwardOverview.html
                                            response.getWriter().write("13");
                                        }else{
                                            if (accountGroup.getGroupSection().equals("Cub") || accountGroup.getGroupSection() == "Cub") {
                                                //Go to next view - AwardOverview.html
                                                response.getWriter().write("14");
                                            }else{
                                                if (accountGroup.getGroupSection().equals("Scout") || accountGroup.getGroupSection() == "Scout") {
                                                    //Go to next view - ScoutAwardOverview.html
                                                    response.getWriter().write("15");
                                                }
                                                if (accountGroup.getGroupSection().equals("Venturer") || accountGroup.getGroupSection() == "Venturer") {
                                                    //Go to next view - VenturerAwardOverview.html
                                                    response.getWriter().write("16");
                                                }
                                                if (accountGroup.getGroupSection().equals("Rover") || accountGroup.getGroupSection() == "Rover") {
                                                    //Go to next view - RoverAwardOverview.html
                                                    response.getWriter().write("17");
                                                }
                                            }
                                        }
                                    } else {
                                        //Is a Non-leader with more than one Youth Member.
                                        //Store the LoginView data for use in subsequent Views.
                                        MySQLConnection.setViewData(accountGroup.getAccountId(), accountGroup.getLevel(), null, null, null, accountGroup.getScoutGroupId(),
                                                null, 0, accountGroup.getGroupSection(), null, null, null, null, accountGroup.getGroupNAS());

                                        //Go to next view - SelectPerson.html
                                        response.getWriter().write("1");
                                    }
                                }
                            } else {
                                //Is a Leader or Administrator.
                                //Store the LoginView data for use in subsequent Views.
                                //Store account information for securely passing between views
                                MySQLConnection.setViewData(accountGroup.getAccountId(), accountGroup.getLevel(), null, null, null,
                                        accountGroup.getScoutGroupId(), null, 0, accountGroup.getGroupSection(), null, null, null, null, accountGroup.getGroupNAS());
                                if (accountGroup.getLevel().equals("National")) {
                                    //National Leader Reporting - NationalOverview.html
                                    response.getWriter().write("12");
                                }else{
                                    if (accountGroup.getLevel().equals("National-Section")) {
                                        //National Section Leader Reporting - NationalSectionOverview.html
                                        response.getWriter().write("11");
                                    }else{
                                        if (accountGroup.getLevel().equals("State")) {
                                            //State Leader Reporting - StateOverview.html
                                            response.getWriter().write("10");
                                        }else{
                                            if (accountGroup.getLevel().equals("State-Section")) {
                                                //State Section Leader Reporting - StateSectionOverview.html
                                                response.getWriter().write("9");
                                            }else{
                                                if (accountGroup.getLevel().equals("Region")) {
                                                    //Region Leader Reporting - RegionOverview.html
                                                    response.getWriter().write("8");
                                                }else{
                                                    if (accountGroup.getLevel().equals("Region-Section")) {
                                                        //Region Section Leader Reporting - RegionSectionOverview.html
                                                        response.getWriter().write("7");
                                                    }else{
                                                        if (accountGroup.getLevel().equals("District")) {
                                                            //District Leader Reporting - DistrictOverview.html
                                                            response.getWriter().write("6");
                                                        }else{
                                                            if (accountGroup.getLevel().equals("District-Section")) {
                                                                //District Section Leader Reporting - ManagementOverview.html
                                                                response.getWriter().write("5");
                                                            }else{
                                                                if (accountGroup.getLevel().equals("Group-Leader")) {
                                                                    //Group Leader Reporting - GroupSummary.html
                                                                    response.getWriter().write("4");
                                                                }else{
                                                                    if (accountGroup.getLevel().equals("Group-Treasurer")) {
                                                                        //Group Leader Reporting - TreasurerGroupSummary.html
                                                                        response.getWriter().write("3");
                                                                    }else{
                                                                        //Go to the view to select a Youth Member
                                                                        if (accountGroup.getGroupSection().equals("Joey") || accountGroup.getGroupSection() == "Joey"){
                                                                            //Joey Award update - JoeyAwardUpdate.html
                                                                            response.getWriter().write("2");
                                                                        }else{
                                                                            //Select a Youth Member - SelectPerson.html
                                                                            response.getWriter().write("1");
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            System.out.println("Invalid");
            loginAttempt++;
            if (loginAttempt > 2){
                MySQLConnection.disableAccount(textBoxAccountName, 0);
                response.getWriter().write("To many attenpts - Account has been disabled.");
            }else{
                response.getWriter().write("Incorrect Account name or Password entered.");
            }
        }
    }
}
Glyn
  • 1,933
  • 5
  • 37
  • 60
  • Where is your LoginView method is present i guess you are not providing proper path. Check in the network tab of dev tools of browser too – Zaki Mohammed Mar 11 '18 at 01:25
  • Hi Zaki, I have added the network tab and the folder structure. – Glyn Mar 11 '18 at 05:33
  • Java file doesn't work in that way with an ajax call you should consider making a web service call or .jsp page have a look over this link https://stackoverflow.com/questions/15944000/calling-a-java-method-in-ajax – Zaki Mohammed Mar 11 '18 at 05:52
  • Hi Zaki, I am using a webservelet as suggested in this link. I have added it to the code above. – Glyn Mar 12 '18 at 07:56
  • See your issue is very basic that it is not able to locate your service you have to try some work around. First change URL to "/LoginView" because as I assume that your Login.html file is one folder under the root. Next is try to use Postman (https://www.getpostman.com/apps) to make a call to your webservice directly without HTML pages so you will come to know the proper URL of your service. Your service is proper just your AJAX call not able to locate it. – Zaki Mohammed Mar 12 '18 at 08:08
  • Thanks Zaki, I have found that the issue is that the java classes are not being generated when I build. – Glyn Mar 17 '18 at 01:59

1 Answers1

0

After a lot of trial and error I found that I needed to delete the folder (AwardTracker_NJE) created by the war every time I generated a new war. Otherwise, the old folder contents were not overwritten.

enter image description here

Glyn
  • 1,933
  • 5
  • 37
  • 60