What could be the reason for the Ajax not successfully partially refreshing my page?

Utilizing ajax in conjunction with javascript for canceling or shipping actions. Upon clicking the cancel link, no immediate change is reflected on the page, requiring a complete refresh to see the updated status. Clicking the ship link yields no result even after refreshing the entire page. The JSP and controller code are displayed below:

JSP:

<script>
    var xmlHttp;
    xmlHttp = GetXmlHttpObject();
    function GetXmlHttpObject()
    {
        var xmlHttp = null;
        try
        {
            // Firefox, Opera 8.0+, Safari
            xmlHttp = new XMLHttpRequest();
        } catch (e)
        {
            // Internet Explorer
            try
            {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e)
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }
    function cancelOrder(orderId, rowID){
        if (xmlHttp == null)
        {
            alert("Your browser does not support AJAX!");
            return;
        }
        var query = "action=cancel&order=" + orderId;

        /* alert(orderId); */
        xmlHttp.onreadystatechange = function stateChanged()
        {
            if (xmlHttp.readyState == 4)
            {
                var status = document.getElementById(rowID);
                alert(status.innerHTML); 
                status.innerHTML = "Canceled";
            }
        };
        xmlHttp.open("GET", "manageUserAccount.htm?"+query, true);
        xmlHttp.send(null);
        return false; 
    }
function shipOrder(orderId, rowID){
        if (xmlHttp == null)
        {
            alert("Your browser does not support AJAX!");
            return;
        }
        var query = "action=ship&order=" + orderId;

        /* alert(orderId); */
        xmlHttp.onreadystatechange = function stateChanged()
        {
            if (xmlHttp.readyState == 4 && xmlhttp.status==200)
            {
                var status = document.getElementById(rowID);
                alert(status.innerHTML); 
                status.innerHTML = "Canceled";
            }
        };
        xmlHttp.open("GET", "manageUserAccount.htm?"+query, true);
        xmlHttp.send(null);
        return false; 
    }

</script>

<c:forEach var="order" items="${user.orders}" varStatus="vs">
        <tr>
                <td>${order.id}</td>
                <td>${order.createDate}</td>
                <td>${order.status}</td>
                <td><a href="manageUserAccount.htm?action=viewDetails&orderId=${order.id}">View Details</a></td>
                <td><a onclick = "shipOrder(${order.id}, ${vs.index})">Ship the order</a><br><a onclick = "cancelOrder(${order.id}, ${vs.index})">Cancel the order</a></td>
        </tr>
</c:forEach>

Controller:

if(action.equals("cancel")){
        OrderDAO orderDao = new OrderDAO();
        System.out.println(request.getParameter("order"));
        Long orderId = Long.parseLong(request.getParameter("order"));
        Order order = orderDao.get(orderId);
        order.setStatus("Canceled");
        orderDao.save(order);
        JSONObject obj = new JSONObject();
        obj.put("cancel", "Canceled");
}
if(action.equals("ship")){
        OrderDAO orderDao = new OrderDAO();
        System.out.println(request.getParameter("ship"));
        Long orderId = Long.parseLong(request.getParameter("ship"));
        Order order = orderDao.get(orderId);
        order.setStatus("Shipped");
        orderDao.save(order);
        JSONObject obj = new JSONObject();
        obj.put("ship", "Shipped");
}

Seeking assistance in resolving this issue. Appreciate any help provided. Thank you!

Answer №1

When I want to verify the response, I typically rely on:

if (xmlhttp.readyState==4 && xmlhttp.status==200)

Could it be possible that you are not receiving the correct status in your response?

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

Having issues with the `onlyCountries` property in the React phone input 2 component when using

I've integrated the react-phone-input-2 library to include phone number fields in my project. I need to provide selected countries only for the country codes. I'm fetching iso2countrycodes from the server and passing them to my Phone input compon ...

The X-frame-Options HTTP response header is ineffective

I've been attempting to implement the X-Frame-Options response header in my application by configuring it on my express server. Utilizing the helmet npm package, I've applied the following code snippet: const express = require("express" ...

The react-bootstrap implementation is not functioning as expected, resulting in an unsupported server component error

Having an issue with an Unsupported Server Component Error while using react-bootstrap with typescript. I've shared the contents of my page.tsx file, layout.tsx file, and the specific error message. layout.tsx file import type { Metadata } from &apos ...

Avoid assigning variables outside of their intended scope in Javascript

I'm struggling to figure out why the assignment of vm.Info variable isn't working as expected. Apologies for the unclear question. vm.getUser = function(email) { vm.Info = '' authentication ...

Modifying hidden field values with JavaScript does not carry over to the server side in Chrome

I created a custom user control that contains a hidden field which is set when a node is clicked on a Tree View Hierarchy control. The following function is responsible for handling the click event of the Tree View: function HandleTreeClick(evt) { va ...

"Using jQuery to ensure multiple sections are ready for manipulation on

After some experimentation, I made an interesting discovery: I can actually include $(document).ready(function(){}); multiple times. For example: $(document).ready(function(){ var abc = "1122"; //do something.. }); $(document).ready(function() ...

Is there a more efficient method for managing Express rendering based on whether the request is an AJAX post or an HTML form submission?

Currently, I am working on an HTML form and incorporating jQuery's AJAX post feature to retrieve information based on the success or failure of a database insertion. Within my code, I have set the ajax post variable to true when utilizing AJAX postin ...

Laravel and Vue: Footer-div component not recognized

After reading an article on Vue.js from this source, I decided to start learning it. Everything was going smoothly with my SPA until I tried to add a new component and encountered this error: Unknown custom element: - have you registered the component c ...

Can you provide guidance on using javascript or jquery to navigate up the DOM tree, focusing only on immediate parent elements and excluding any siblings or other relatives?

I am struggling with climbing up the DOM tree to access the parent, grandparent, and great-grandparent nodes of a specific node without including their siblings. I have attempted using recursion to traverse the tree but have not been successful in getting ...

The Angular routing feature seems to be malfunctioning

I have encountered a frustrating issue with AngularJS routing and despite countless searches for solutions, I am still facing the same problem. In my root folder at c:\intepub\wwwroot\angular\, I have three files that I am testing under ...

Loop through XML nodes in Node.js

I am faced with a challenge of extracting specific data from a large XML document. The document can be accessed via the following link: https://pastebin.com/mNXWt7dz My goal is to parse the XML structure in order to retrieve values for each client-mac, cl ...

The test() function in JavaScript alters the output value

I created a simple form validation, and I encountered an issue where the test() method returns true when called initially and false upon subsequent calls without changing the input value. This pattern repeats with alternating true and false results. The H ...

It appears that the ngRepeatWatch feature is causing a slowdown in Batarang

After reviewing the Batarang analysis of my AngularJS app, I have discovered the following: ngRepeatWatch | 64.2% | 136.0ms Surprisingly, this instruction takes up 10 times more time than the next reported instructions. Could this indicate that I am pot ...

I am interested in accessing a file located on my D: drive through the use of AJAX

The file I uploaded can be found at D:\eclipseworkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\DatasetFileUpload\myNewFolder However, I am having trouble reading a file from the above locati ...

Incorporating a classList.toggle into a snippet of code

button, p, h1, h2, h3, h4, h5, a{ /* Define specific elements to use "fantasy" font */ font-family: Tahoma; } #main_body{ margin: 0px auto; background-color: #dedede; } #top_body{ /* Remove margin for simplicity */ } #t ...

IE compatibility with CSS2 selectors

Is there a plugin, preferably jQuery, that can enable the use of CSS2 selectors like 'parent > child' and 'element:first-child' in my stylesheet for IE6, which doesn't seem to support them natively? ...

EJS functionality is operational, however, the HTML content is not displaying

I'm currently developing a timer using Express.js and EJS. My goal is to update the HTML dynamically, but I seem to be encountering an issue where nothing gets displayed. Strangely enough, I can see the output in my CLI with console.log. <div id ...

What is the best way to extract all "conditions" nested under the key "logic" at the 0th index in a JSON object?

I need to manipulate a nested object by removing every "condition" where the key is "logic" and the value is 0 from it. Here is an example of the object structure: Original input: [ { "conditions": [ { "logic": "AND", "paramet ...

What is the best way to ensure a superfish dropdown menu is fully responsive?

Currently, I have implemented a superfish dropdown menu within the skelton framework. My goal is to make it functional on mobile devices as well. The issue I am facing is that by default, the dropdown items appear but they hover over the items below them ...

Reload the MEN stack webpage without the need to reload the entire page

I am in the process of developing a data analytics dashboard using the MEN stack (MongoDB, Express.js, Node.js). I have successfully implemented functionality to display real-time data that refreshes every 5 seconds without the need to reload the entire ...