Debugging and ensuring the functionality of Cordova (Phonegap) HTTPS connections

There is an HTTPS site with an API that needs to be accessed.

I need to work from Cordova (AngularJS) with its HTTPS API.

Additionally, I want to debug the AngularJS app in a web browser (Chrome) because it's much quicker compared to rebuilding and deploying.

Here is some simple code snippet:

$http({
    method: 'POST',
    crossDomain: true,
    xhrFields: { withCredentials: false },
    url: 'https://.../api/Auth',
    data: { email: user, password: password }
    }).
    then(function (response) {
        console.log("0", response);
    }, function (response) {
        console.log("1", response);
    });

$.ajax({
    type: "POST",
    url: 'https://.../api/Auth',
    xhrFields: {
        withCredentials: true
    },
    crossDomain: true,
    data: { email: user, password: password },
    success: function (response) { console.log("2", response) },
});
  1. Result in Cordova debug (Visual Studio, Windows-x64)

First request

HTTP500: SERVER ERROR - The server encountered an unexpected condition that prevented it from fulfilling the request. (XHR): GET -

This error occurs twice, once for $http and once for $.ajax.

Second and subsequent requests - Return correct response (also twice)

  1. Result in Chrome

All requests

a) $http uses the OPTION method instead of POST

b) $http error: XMLHttpRequest cannot load . Response for preflight is invalid (redirect)

c) $.ajax uses the correct POST method

d) $.ajax error: XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access.

Questions

  1. How can I work with HTTPS in Cordova and Chrome using the same code?

  2. How do I skip the first request in Cordova and work with the second one directly?

Answer №1

Starting from Cordova version 5, it is necessary to utilize the Cordova Whitelist Plugin.

After installing the plugin, adjustments must be made to your project's config.xml file. To handle network requests, include <access> tags like this:

<access origin="http://*.google.com" />

Furthermore, configuring the Content Security Policy for your app is essential. It is recommended to consult the Cordova Whitelist Plugin documentation for assistance.

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

Stop the print dialog box from causing the page to refresh

I have implemented a print button for an invoice: </script> <!--Function for printing invoice--> <script> function printpage() { window.print() } </script> <button id="print" name="print" class="btn btn-info" onClick="pri ...

Applying CSS styles to a page depending on certain conditions

Currently, I have a page in SharePoint that can function as a pop-up. I am using JavaScript to identify whether it is a pop-up by checking if window.location.search.match("[?&]IsDlg=1"). However, I am struggling to figure out how to insert CSS based on ...

When an SVG image is embedded, its color may not change even after being converted to an inline SVG

I've inserted an SVG using an img tag. When hovering over it, I want the fill color of the SVG to change. I attempted to convert the SVG to inline SVG following this method, but it doesn't seem to be working as expected. No console errors are b ...

`How can a child component in Next.js send updated data to its parent component?`

Currently diving into Next.js and tinkering with a little project. My setup includes a Canvas component alongside a child component named Preview. Within the Preview component, I'm tweaking data from the parent (Canvas) to yield a fresh outcome. The b ...

Problem encountered with JavaScript getter on iOS 5

While implementing JavaScript getters in my iPad-optimized website, everything was working perfectly until I updated to iOS 5. Suddenly, the site stopped functioning properly. After thorough investigation, I discovered the root cause of the issue. I have ...

The Express Validator is unable to send headers to the client once they have already been processed

I recently integrated express-validator in my Express JS project, but encountered a warning when sending invalid fields to my api: UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client This ...

Deleting a nested object from an array within another object

Recently, I delved into the world of Redux and have found it quite fascinating. However, I am currently facing a dilemma where my new reducer function inadvertently changes the type of a state variable, which is not what I intended. The desired structure ...

Executing a server-side Java function from client-side JavaScript or jQuery on a JSP page

My JSP file has a dropdown list where I can select different entity kinds. Upon selecting an entity kind, I want to populate another dropdown list with the field names associated with that entity kind. This requires calling a JavaScript function when chang ...

Discover the method to determine the total count of days in a given week number

I am developing a gantt chart feature that allows users to select a start date and an end date. The gantt chart should display the week numbers in accordance with the ISO standard. However, I have encountered two situations where either the start week numb ...

Troubleshooting ASP.NET MVC3: The mystery behind why my custom validation attributes always seem to fail

After completing several tutorials, I have successfully implemented my models from a library file (dll). Everything seems to be functioning correctly except for one issue. Here is my model: public class RoomBookingInsert { public Int32 CostCentreNo ...

Heroku is rejecting the Discord token, but it is functioning properly in Visual Studio Code

I am facing an issue with an invalid token error on Heroku, even though the token in my main.js file on Git is the same as the one I have in Visual Studio Code. Interestingly, Heroku claims it's an invalid bot token while the Discord bot token from VS ...

Fixing PHP Call Internal Server Error Using JQuery Ajax

Being fairly new to this subject, I've managed to make it work when passing only 1 $_POST variable. However, now that I'm attempting to pass 2 variables, I'm encountering an Internal Server Error. After some investigation, it's clear t ...

Issue with Contact Form 7: AJAX submission failure leading to redirection to 404 error page

I've been hard at work building a new website that involves using the Wordpress plugin Contact Form 7 to manage form submissions and ideally show a response through AJAX. However, I'm struggling to get the AJAX functionality to function properly ...

Logging into Angular using your own Oauth2 provider

I have developed my own Oauth2 provider using Rails and now I want to integrate it with Angular. Currently, when I make a request like this: http://www.myservice.com/authenticate?callback="A PATH WHERE I WILL RECEIVE THE AUTH_TOKEN(example: localhost:888 ...

Difficulty with linking a CSS property to an HTML element using JavaScript

I'm currently working on building a custom carousel from scratch. Instead of using pre-made code snippets, I decided to challenge myself by creating one using setTimeout in JavaScript. However, I seem to be encountering an issue that I can't quit ...

Bcrypt.compare function working in code but not functioning in chai/mocha tests

I have integrated node.js backend into my project. For encrypting passwords, I am utilizing the bcrypt library. To compare the string password from the request with the hashed password in the database, I am using the bcrypt.compare function. The bcrypt.com ...

The chosen value remains constant even after altering the selected option

I am trying to create a scroll-down bar that allows users to select different options, and once an option is selected, its value should appear in the bar. However, I'm facing an issue where the value does not change according to the selection: const [ ...

When a radiobutton is clicked, a jQuery call to a PHP function triggers an AJAX request which results in a JavaScript function becoming unrefer

Currently, I have a situation where two radio buttons are representing different products. When one of them is clicked, the goal is to update the price displayed on the website based on the selected product. Everything seems to be working fine when using t ...

Issue in Laravel Ajax where only one product's subtotal is updating and not functioning properly for multiple products

When using this shopping cart, the quantity is supposed to be multiplied with the price to display the subtotal. Everything works fine when only one item is added to the cart. However, an error occurs when multiple items are inputted: "Integrity constrain ...

Using the WordPress function wp_mail along with Ajax and ValidationEngine to perform database insertion through the $wpdb object

Seeking guidance on a complex issue, I present a query regarding a contact form integrated within my home.php file. The code structure is as follows: HTML: <form id="js-calculator" name="calculator" action="" method="post"> <input type="ema ...