After the successful execution of the AJAX call, the webpage is displaying an error message instead of its intended content

Upon successfully making an ajax call by clicking the submit button, I am encountering the following error page in my browser with no visible errors in the console:

This page isn't working If the problem continues, contact the site owner. HTTP ERROR 405

I'm relatively new to javascript, jquery, and ajax calls so any suggestions on how to resolve this error would be greatly appreciated.

Here's a snippet of the HTML code being used:

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/index.css" />
    <title>Document</title>
</head>
<body>
       <div>
        <h1 class="myclass">HELLO! WELCOME</h1>
    </div>
    <div id="maindiv">
        <form id="loginform" method="post">
            <div class="form-group">

                <input type="number" class="form-control" id="inputphone" placeholder="Enter phone number">
            </div>
            <div class="form-group">
                <input type="password" class="form-control" id="inputpassword" placeholder="Enter Password">
            </div>
            <div class="forgot-password">
                <a href="">Forgot password</a>
            </div>
             <button id="submit" class="submit-button" >Submit</button> 
           <!-- <input type="submit"value="Login"/>-->
        </form>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script  src="js/index.js" type="text/javascript"> </script>    
</body></html>

And here's a look at the JS code that accompanies it:

$('#submit').click(function () {
    alert(".click() called.");
    var inputphoneno = document.getElementById("inputphone").value;
    var inputpassword = document.getElementById("inputpassword").value;

    const jdata = {
        "phonenumber": inputphoneno,
        "password": inputpassword
    };

    try {
        $.ajax({

            type: "POST",
            url: "http://localhost:8085/Smatr/signIn",
            data: JSON.stringify(jdata),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            
               success: function (data) {
               console.log(data)
               alert(data);
         }   
           
        });
    }
    catch (ex) {
        alert(ex);

    }

});

Answer №1

Make sure to include the attribute type="button" in your button element to avoid accidentally submitting a form. Without this attribute, the button will default to acting as a submit button.

<button id="submit" type="button" class="submit-button">Submit</button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is causing React to make multiple API calls when loading the page?

I have a page set up with hooks to retrieve data from a third-party API. Once the data is fetched, I perform various calculations on it before passing it as props to the component. However, it appears that the API is being called multiple times for each ca ...

React Express Error: Unable to access property 'then' of undefined

I'm facing an issue while trying to server-side render my react app for users who have disabled JavaScript and also for better search engine optimization. However, I am encountering the following error: TypeError: Cannot read property 'then' ...

Changing URI to File Object using JavaScript

I have successfully retrieved the URI of a selected file in my Ionic app using the cordova-fileChooser plugin. https://i.sstatic.net/NxXti.jpg Next, I used the cordova-plugin-filepath to obtain the absolute path of the file from the nativeURL on the phon ...

Press on a specific div to automatically close another div nearby

var app = angular.module('app', []); app.controller('RedCtrl', function($scope) { $scope.OpenRed = function() { $scope.userRed = !$scope.userRed; } $scope.HideRed = function() { $scope.userRed = false; } }); app.dire ...

Error: Unable to iterate over the elements of `this.state` as it is

NEW UPDATE I encountered an issue with the response being an array, but it was actually coming from the backend (Express/Build folder) Revisiting an old issue that I faced some time ago. In my development environment, everything works fine. But once I d ...

Tips for managing asynchronous file uploading in uploadify

I'm currently facing an issue with my uploadify plugin setup on my webpage. Below the plugin, there is a description field along with a textbox where users can input text. There is also a submit button positioned below these elements. My challenge lie ...

Ensure prototype is easily accessible within vuex

Within my app.js file, I implemented a functionality to enable translation in Vue: Vue.prototype.trans = string => _.get(window.i18n, string); This feature works perfectly when used in my Vue files: {{ trans('translation.name') }} However, ...

Submitting Content from CKEditor

Currently, I am working on a web form where I am $.posting data that includes a CKEditor textarea. $(document).ready(function() { CKEDITOR.replace('html'); CKEDITOR.config.htmlEncodeOutput = true; //I have noticed this line ...

"Using jQuery to animate opacity can result in the presence of lingering ghost

Whenever I try to animate the opacity of certain elements, it seems like the animation doesn't complete properly. The tooltips end up looking weird (to replicate, just move your mouse over the divs): Boxes 1 and 2 still have remnants over them. Here ...

Designing borders for tables

My current table styling approach is as follows: #content table { width: 100%; margin-top: 1em; border-collapse: collapse; border: 1px solid #222; } #content table td { border: 1px solid #888; padding: .3em; } I aim to create tab ...

Update Anchor Tag upon Every Click using Javascript

I am having issues with my ecommerce site where every time a user clicks on "Add to Pallet" (we refer to it as a pallet instead of a cart), the page refreshes and positions itself to the div of that product. You can try it out on our website: . This functi ...

How can I dynamically update a <ul> list with AngularJS when a button is clicked or user inputs something?

For those interested, I have created a JSFiddle that can be viewed here I am trying to find a solution to retrieve user input (filename) from the input box and add it as an item in a list within the <ul>. I attempted to use a .push directive alongsi ...

The function you are trying to execute in jQuery Mobile is not defined

I'm currently working on creating an external panel, but I've encountered some difficulties. Below is the HTML code: <html> <head> <title>My Page</title> <meta name="viewport" content="width=devi ...

Acquiring the selector value from a tag

To summarize: This snippet of code: for(let i = 0; i <= items.length; i++){ console.log(items[i]) } Produces the following output: <a class="photo ajax2" target="_blank" href="/profile/show/3209135.html" data-first="1" data-next="3206884"> ...

Tips for preventing needless re-renders in React functional components when dealing with "static components"

In my React functional component, I have a feature that displays a list of tags and posts along with some static text and decorations. The currently selected tag is stored in a state using the useState hook. Posts are fetched through Apollo's useQuery ...

What is the best way to retrieve the value of this event in a React component?

Is there a way to retrieve the value of an input field? Typically, I would use event.target.value to do so. However, in this case, that method is not viable because the path of event.target leads me to an li element instead: https://i.sstatic.net/0iB9v.pn ...

JavaScript if statement to check for either one, but not both

Hey there, fellow developers. I'm currently encountering a mental block and struggling to find a solution for my issue. Here is the code snippet in question: if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0)) { ...

Tips for utilizing Sass and CSS Modules within create-react-app

I've been using FileName.module.scss to style my react elements like this: // Component styling with SCSS import React from "react"; import Aux from '../../hoc/Aux'; import classes from './Layout.module.scss'; const lay ...

Display a tooltip when you click on a different element

I have a unique feature that I want to implement on my website. The idea is to display a tooltip when a user clicks on a specific div with the same ID as the tooltip. This will be particularly useful for interactive questions where users can click and reve ...

Leveraging information within a handlebars function

I want to create a Handlebars helper that will allow me to show an element x number of times, with the value of x determined by the data passed to the template. I came across some code on this page that uses the #times function. However, instead of the lo ...