Having trouble with Cordova and JQuery AJAX integration?

After researching for a couple of hours, I discovered that my cordova app (CLI 5.3.3) is displaying a "page not found" error when making AJAX calls through jQuery.

Despite following all the steps in the whitelist plugin (https://github.com/apache/cordova-plugin-whitelist), the issue persists.

I have already added the necessary lines to the config.xml file:

<access origin="*" />
<allow-navigation href="*" />

I have also included Content Security Policy (CSP) like this:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' https:">

My AJAX request code looks like this:

$.ajax({
                    beforeSend: function() { $.mobile.loading("show"); }, //Show spinner
                    complete: function() { $.mobile.loading("hide"); }, //Hide spinner
                    url: weburl+"lgoin.php",                        
                    data: { email: $("#txtemail").val(), password: $("#txtpassword").val()},
                    type: "POST",                       
                    success: function(data) {                       
                        var response=$.parseJSON(data);                         
                        }                           
                    },                      
                    error: function (jqXHR, exception) {
                        var msg = '';
                        if (jqXHR.status === 0) {
                            msg = 'Not connect.\n Verify Network.';
                        } else if (jqXHR.status == 404) {
                            msg = 'Requested page not found. [404]';
                        } else if (jqXHR.status == 500) {
                            msg = 'Internal Server Error [500].';
                        } else if (exception === 'parsererror') {
                            msg = 'Requested JSON parse failed.';
                        } else if (exception === 'timeout') {
                            msg = 'Time out error.';
                        } else if (exception === 'abort') {
                            msg = 'Ajax request aborted.';
                        } else {
                            msg = 'Uncaught Error.\n' + jqXHR.responseText;
                        }
                        alert(msg);
                    },
                });

Every time I make an AJAX call, it ends up with the error message "Requested page not found. [404]" in the error callback.

Note: I have tested the webservice with the Avance REST API extension and it works correctly.

If anyone can help me resolve this issue, I would greatly appreciate it.

Thank you for your time and consideration. -Naitik

Answer №1

After inserting the following line into the application tag of my AndroidManifest.xml file:

android:usesCleartextTraffic="true" 

The issue was resolved successfully.

Answer №2

Essentially, the key point to note is that updating cordova-whitelist-plugin is necessary.

To install the plugin, you can use

cordova plugin add cordova-plugin-whitelist

or by including

<plugin name="cordova-plugin-whitelist" spec="1" />

in your config.xml file, and then configure it with

<allow-navigation href="*" />

instead of the old <access origin="*" /> tag.

For more information, please refer to this link.

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

How to use AJAX script to call a non-static page method in ASP.NET

Is it possible to achieve this task without utilizing the UpdatePanel feature? ...

Create an HTML table row that includes a value along with its related sibling and parent values

I need to create an HTML table that compares segments in a JSON object. The format should display the segments along with their measures organized by domain group and vertical: ------------------------------------------------------------------------------ ...

How can we compress videos on an Android device prior to uploading them through a web browser?

Can video compression be done prior to uploading via a web browser? For example, in iOS devices you can choose a video using the HTML input type file tag and iOS will automatically compress it before uploading. Are there any JavaScript or jQuery libraries ...

"Exploring the possibilities of cakePHP in integrating Ajax

After setting $this->layout = 'ajax', Cake still requires the view file for that action to be present. I'm wondering, is it necessary to create that view file, or is there a workaround? I want to avoid having one-liner view files for aja ...

Is there a way to store image URLs in a fashionable manner?

Hey there! I've been working on creating a HTML page that showcases multiple images. Everything is running smoothly on my localhost, but when I try to access it online, the images take forever to load. Any suggestions on how I can cache image URLs in ...

Require assistance in understanding JSON data in the form of a variable

Apologies for any language barriers, but I have encountered a problem that I need help with. I am trying to create a highchart from an AJAX call. The following code works perfectly: chartOptions = { chart: { renderTo: 'grafica1', type: 'ar ...

Validation of forms and submission of forms through AJAX with the use of jQuery and CodeIgniter framework

My current code for adding comments is as follows: <div id="2" class="789678"> <div id="dynamic2"> <span class="error" style="display:none">Please Enter Valid Data</span> <span class="success" style="display: ...

Transforming Input Background Color with jQuery based on Matching PHP Array Entry

I am looking for a solution to implement form validation in PHP. The form has 3 fields: user, id, and email. When a user enters their name or id in the respective input fields, I want to check if the entered value matches any of the values in the PHP arra ...

Retrieval of jQuery remove() method

Essentially, I am utilizing an OnClick function to delete a DIV. Once the OnClick is triggered, it invokes the remove() jQuery function to eliminate the div. Below is my code that implements the removal using remove(): <div id="add"> <button typ ...

Perform the action when the button is clicked

I'm struggling with finding a solution to this problem. I have an input field and a submit button. My goal is to update the value of a variable to match the value in the input field when the submit button is clicked. Additionally, I need to execute a ...

A useful Javascript function to wrap a string in <mark> tags within an HTML document

I have a paragraph that I can edit. I need to highlight certain words in this paragraph based on the JSON response I get from another page. Here's an example of the response: HTML: <p id="area" contenteditable> </p> <button class="bt ...

jQuery preventing form submission after returning false

There's a small issue that I need help with. After submitting the form and receiving a false response, it prevents resubmission. For example, if a user forgets to input their name, they will see an error message asking them to do so. However, the u ...

How to achieve the wrapping functionality in ReactJS that is similar to

Is there a ReactJS equivalent to jQuery's wrap method? I want to wrap menuContents with the following element: <ul className="nav nav-pills nav-stacked"></ul> The contents of menuContents are generated like this: let menuContents = thi ...

Filtering Wordpress content on the fly with a dropdown menu (utilizing php and ajax)

Objective: My goal is to create a dynamic webpage that allows visitors to choose a specific month and year from a dropdown menu. The content (posts) on the page should automatically change based on the selected values. Currently, I am using the following ...

Updating the form action using jQuery when submitting

One of the challenges I am facing involves modifying the action URL of a filter form using jQuery. <form id="filter_form" action=""> <div style=" bottom: 0; z-index: 1; width: calc(100% - 20px);"> <input id="filter_submit" type ...

Is it considered safe to display the error message from an AJAX call for security reasons?

When I make my ajax calls, they usually follow this pattern: $.ajax({ type: 'POST', url: '/Controller/DoSomethingSpecial', contentType: 'application/json;', ...

yii ajaxurl customization for table sorting

Here is some code I have: ajaxUrl : '<?php echo Yii::app()->createUrl("abc/def");?>&page={page}&size={size}&{sortList:col}&{filterList:fcol} ', Referenced from: http://mottie.github.io/tablesorter/docs/example-pager-aj ...

"Implementing a Jquery event to handle the closing of a select

Is there a way for me to detect when the select tag is closed by clicking on the screen elsewhere? I can easily hook into the change event of the select fine, which will close the select. However, other methods such as body click, body focus, select blur, ...

Using Vue.js to make AJAX requests

I am facing an issue while trying to fetch data from an API endpoint api/data which returns an object. However, when I run my application, nothing shows up in the console and there are no XHR requests visible in the network tab. There are no warnings or er ...

Adjusting the Pace of a CSS Marquee

My CSS marquee effect is working perfectly, but I am having trouble with the speed. The issue arises when the text length varies - shorter text takes longer to scroll while longer text scrolls quickly. This inconsistency is due to the animation duration an ...