The AJAX request to the REST service is failing to trigger the success function in the AJAX call

Struggling with some issues related to AJAX, specifically when calling a Java REST server that I've developed. While I am relatively new to AJAX, I have put in quite a bit of effort searching for solutions. The problem arises when making a call from a test HTML page within the REST server (running on Jetty). The following function works successfully using .get:

        $('#getRVLakeForm').submit(function(e) {
            var handle = $('#insertRVLakeHandle').val();
            var lakekey = $('#RVLakeKey').val();
            var temp = $('#RVLakeTemp').val();
            var speed = $('#RVLakeWindspeed').val();
            var degrees = $('#RVLakeWindDegrees').val();;
            $.get('${pageContext.request.contextPath}/api/recent/lake/' + handle + "/" + temp + "/" +  speed + "/" + degrees + "/" + lakekey, function(data) {
                alert(data.lake[0].oid);

                $('#RMLakeResponse').text("back");
            });

The JSON returned by the REST server is as follows:

{"user":[],"lake":[{"oid":"519b9a1a3004f8fa1287502a","type":"Lake","handle":"nightstalker","viewedhandle":"","viewedlaketag":"TXFORK","viewedusername":"","timestamp":1369152026668}]}

While this call successfully executes the REST API and receives the expected JSON response, the issue arises when attempting to make the same call from an HTML/PHP application running under MAMP. The outbound AJAX call works fine—I can debug the Java REST server and see the call coming in, executing as intended and producing JSON to send out. However, the problem lies in the fact that the success function in the AJAX call never gets called (nor does the error function).

    urlrecent = "http://localhost:8080/server/api/recent/lake/" + handle + "/" + temp + "/" +  speed + "/" + degrees + "/" + lakekey;

    $.ajax({
        type: "GET",
        contentType: "application/json",
        dataType: "jsonp",
        url: urlrecent,
        success: function (data) {
            alert("hello there!");
            alert(data)
        },
        error: function () {
            alert('failed');
        }
    });

I even tried using getJSON instead, but no luck so far. In the meantime, I'll continue exploring Stack Overflow and other online resources for a potential solution. I also experimented with setting the datatype to just "json."

Thank you in advance.

=====

Yes, I messed up the contentType, but it didn't seem to have any impact.

Answer №1

Have a go at using IE8 and configuring dataType: "html"

urlrecent = "http://localhost:8080/server/api/recent/lake/" + handle + "/" + temp + "/" +  speed + "/" + degrees + "/" + lakekey;

    $.ajax({
        type: "GET",
        contentType: "application/json",
        dataType: "html",
        url: urlrecent,
        success: function (data) {
            alert("hello there!");
            data = JSON.parse(data);
            alert(data)
        },
        error: function () {
            alert('failed');
        }
    });

If you encounter a syntax error at data = JSON.parse(data) or see "hello there" in the alert, it may be an encoding issue. In such cases, consider using

response.setCharacterEncoding("UTF8")

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

Tips for Deserializing Complex JSON Objects in C#:

I have the ability to deserialize JSON and insert data into a database table, but I am struggling with inserting the correct "ParentID & ParentFullPath" within the table. Is there a way to achieve this using recursion or without recursion? Here is the met ...

Error encountered while attempting to make AJAX call: "`$.ajax` parse error on JSON object: {"vErrorsFound":true,"vMessage"

I am trying to update my code from using jQuery 1.3.2 to jQuery 1.5, but I am facing issues with parsing JSON data. I have a PHP script that returns the following JSON object using json_encode: {"vErrorsFound":true,"vMessage":"Login Failed"} I have exper ...

Is it possible to move between textboxes using arrow keys in HTML?

Is there a way to navigate through textboxes using arrow keys in HTML without relying on jQuery? Possibly utilizing JavaScript, HTML, CSS, or another method? Thank you for your help! <body> <form> <input type="text"&g ...

Pause the for loop until all nested asynchronous database calls are completed

Currently, I am utilizing the listCollection method within mongodb to loop through each collection that is returned using a query with find. The issue arises when I attempt to construct an object within the loop that I intend to return with response.json ...

Set a maximum height for an image without changing its width dimension

I am facing an issue with an image displayed in a table. The image is a graph of profiling information that can be quite tall (one vertical pixel represents data from one source, while one horizontal pixel equals one unit of time). I would like to set a ma ...

Is there a way to disable or deactivate all jQuery functions at once?

I have developed an application with push state functionality and it is running smoothly. However, I am facing an issue where my jQuery functions are being triggered multiple times in certain cases. This happens because every time I call push state, the sp ...

How to retrieve a random element from an array within a for loop using Angular 2

I'm in the process of developing a soundboard that will play a random sound each time a button is clicked. To achieve this, I have created an array within a for loop to extract the links to mp3 files (filename), and when a user clicks the button, the ...

Selenium unable to interact with Javascript pop-up box

I am currently working on automating a feature for our web application, specifically a form of @mentioning similar to Facebook. On the front end, when a user types @ into a text input, the API is called to retrieve the list of users and display them in a b ...

Font family 'anticon' is not recognized

While following a coding tutorial on YouTube, I encountered an error message that has me stumped. Despite having the correct import statement and dependency installed, the issue persists. Error message in iOS simulator: https://i.stack.imgur.com/LOVCQl. ...

Is there a way to dynamically set the active panel of a JQuery Accordion when making a call?

Currently, I am faced with a scenario where I need to implement a greybox popup window using jQuery Accordion from the main page via links. I am curious to know if it is doable to specify a default active panel for the accordion when calling it. Below is ...

Endless loop caused by Angular UI-router's promise resolving

I'm attempting to retrieve data from my SQLite database before loading a view using resolve when defining the state in ui-router. Currently, this is how I've set up the state: .state("menu", { templateUrl: "templates/menu.html", control ...

Provide two values and complete the third one

I have a form with three input fields. I want to fill out two of the fields and have the third field automatically filled in. Here is how it should work: - I fill out the first and second fields, and the third field calculates itself - I fill out ...

A Duplicate Invocation of the JQuery Ajax Function Utilizing Identical Data

I'm creating a basic webpage that serves as a task list. The page allows users to input new tasks through a form, which is then sent to the server via POST request. After receiving (almost) identical data back from the server, it adds the task to a li ...

Tips for transferring data from Controller to View using IEnumerable Model in MVC

Currently, I have a view containing 1700 records that need to be paginated using ajax for lighter page loading. The paging functionality works smoothly, bringing in a new set of records based on the selected page. Issue: The problem arises when displaying ...

Grails 3.1.9 does not support displaying JavaScript

Having trouble getting the datepicker to display using JavaScript <script> $( "#invoiceDate" ).datepicker({ inline: true, dateFormat: "yy-mm-dd", onSelect: function(datetext){ datetext = datetext+" 00:00:00.0" ...

Remove the color options from the Material UI theme

Can certain color types be excluded from the MUI palette in MUI v5? For example, can background and error colors be removed, allowing only colors defined in a custom theme file to be used? I attempted using 'never' but it did not provide a solut ...

Guidelines for deploying a node.js REST API application successfully in a production environment

As I venture into the world of node.js apps, I find myself faced with a dilemma. I've created a REST API using node.js that functions perfectly on my local machine. However, when I attempt to build it using webpack, I'm uncertain about how to run ...

Conditional jQuery actions based on the selected radio button - utilizing if/else statements

This task seemed simple at first, but I quickly realized it's more challenging than expected. Apologies in advance, as Javascript is not my strong suit. My goal is to have the main button (Get Your New Rate) perform different actions based on whether ...

Sending a sound recording to the express js server with the help of multer

I'm currently working on a project where I need to record audio and save it in my local directory (uploads folder) using express js and multer. The recording part is working fine with mic-recorder-to-mp3, but I'm facing an issue with saving the r ...

Why is the jQuery ajax call throwing an error about sendToValue not being defined? What could be causing this issue in the jQuery code

I'm currently learning about jquery and ajax calls, trying to use the code below to retrieve JSON values from a PHP page. $(function(){ $('#search').keyup(function() { sendValue($(this).val); }); }); function sendValue(str) { $.pos ...