Issue with Phonegap FormData not being sent when using Cross-Origin Resource Sharing (

I have developed a mobile app using phonegap 3.5 and implemented a CORS request in the following manner:

function createCORSRequest(method, url, asynchronous) {
    var xhr = new XMLHttpRequest();
    //var n = currentTime.getTime();
    var n = 'nocache=' + generateRandomString(10);
    if (url.indexOf('?') == -1) {
        n = '?' + n;
    }
    else {
        n = '&' + n;
    }
    if ("withCredentials" in xhr) {

        xhr.open(method, url + n, asynchronous);
    } else if (typeof XDomainRequest != "undefined") {

        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {

        xhr = null;
    }
    return xhr;
}

Then, I proceed to create a request object:

var url = base_url + 'login.php';
var xhrLogin = createCORSRequest('POST', url, false);
var formData = new FormData();
formData.append('loginemail','<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="345574561a575b59">[email protected]</a>');
formData.append('pwd','mypassword');
xhrLogin.send(formData);

On the server side, the PHP code looks like this:

$postdata = '';

foreach($_POST as $key => $value)
{
    $postdata .= ' ' . $key . '=' . $value . ';';
}
if (strlen($postdata) == 0) {
    sendResponse(200, 'Error: No post data ' . $postdata, 'text/html');
    return;
}

When testing this code directly from a browser, it functions properly regardless of the base_url configuration. However, when running it through the phonegap server, issues arise with posting data to the production server while working fine with a test server. Network monitoring indicates the data is being sent in both cases.

The behavior is different on Android/iOS devices compared to running on the phonegap server or pointing to the production server. Could it be related to PHP versions?

Update: Testing from home resulted in successful communication with both servers, suggesting a potential proxy issue.

Update2: Using a Mac for phonegap serve resolved the problem, hinting at possible OS-related considerations.


Notably, the version discrepancy between installations – 3.5.0-0.21.14 at work and 3.5.0-0.20.4 at home – seems to impact functionality. Installing the earlier version at work fixed the issue.

Interestingly, CORS requests function correctly in a Ripple Emulator when disabling the cross domain proxy feature.

To resolve similar problems:

$ npm install -g <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc8c949392999b9d8cbccfd2c9d2ccd1ccd2ceccd2c8">[email protected]</a>

Answer №1

To resolve the issue, we incorporated the whitelist plugin into the Android build, granting access to the specified URLs listed in config.xml

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

Is it possible to automatically update the database information through a PHP script and AJAX?

I'm currently utilizing this function to retrieve database rows via ajax. function ajax_search(){ $("#search_results").show(); var search_val=$("#search_term").val(); $.post("find.php", {search_term : search_val}, function(data){ if (data.length ...

An alternate solution for achieving synchronous behavior in form submit button click event handling without using the async: false option

Operating with a login form is essential, and our current practice includes utilizing an AJAX call to validate information before proceeding with the login submission. Here is the existing code snippet being employed: (function ($) { var errorMessageH ...

What is the best way to display JSON within a partial?

Within my Rails app, I have implemented a JavaScript graph. Whenever I click on a circle in the graph, it displays the name of the corresponding user. Now, my goal is to retrieve additional information from the related database table based on this user&apo ...

The primary objective is to ensure that the input remains contained within the code

I am having issues with my PhoneGap 2.2.0 and Android 4.0.4 application. Whenever I click the input, a div opens above it. Here is a snippet of My Input and My Div: <input type="text" onclick="OpenDiv();" id="MyInput" /> <div id="MyDiv"& ...

Analyzing: Sending the uploaded file or image to the backend server

Included in my HTML is a form: form.html: <form method="post" enctype="multipart/form-data" action="/"> <input type="file" name="pic" id="imgupload"> </form> <script> document.getElementById("imgupload").onclick = function ...

Converting Unix time to yyyy-mm-dd format using JavaScript

Can someone assist me with converting Unixtime to a specific date format? Check out my current code below: var date = "2014-05-01"; var indexPie = Date.parse(date); I want indexPie in the yyyy-mm-dd format. However, when I try this line of code: var new ...

Images are not loading in NextJs image component on a Digital Ocean deployed application

I recently encountered an issue with my NextJs project. While using the NextJs Image Component for images, everything worked perfectly fine when running locally. However, after deploying the project on Digital Ocean, all the images served through the Next- ...

Modifying a variable within an arrow function does not result in the variable being changed when checked outside of the arrow

I am currently developing an application with Angular and Typescript where I need to update the value of a variable inside a function. To retrieve the data required, I'm utilizing a service. Below is the code snippet for reference: let isDataAvailab ...

Having trouble with the accordion close animation. It smoothly opens with animation but doesn't close as smoothly, without animation

As someone who is new to javascript, I have a query that may seem basic. I've successfully implemented an open animation, but unfortunately, the close animation isn't functioning as expected. Even after trying to reverse the animation, it doesn& ...

Ways to display information using a show/close button in React

I am currently immersed in the learning process of React. My goal is to display information about different countries using a toggleable button. However, I have encountered some obstacles along the way. There's an input field that triggers upon enteri ...

Leverage the navigator clipboard feature to save an image in PNG format from a canvas

I've been attempting to save a canvas blob to the navigator clipboard graphicDiv.addEventListener( 'click', (event) => { const canvas = <HTMLCanvasElement> document.getElementById('canvas'); can ...

Exploring the Power of Integrating SignalR 2 with a Database

After going through a tutorial on creating a basic messaging web application using SignalR and MVC, I realized that the tutorial did not cover how to store messages in a database or display previous messages to users. The main code snippet from the tutoria ...

What is the best way to run a JavaScript function once another function has finished executing?

I am looking to ensure that a JavaScript function runs only after another one has finished executing. Here is the code I currently have: $(window).load(function(){ $('#dvLoading').fadeOut(2000); }); window.addLoadEvent = function() { $('#p ...

Fixed Navigation - Employing stickUp.js extension

* { box-sizing: border-box; } body { margin: 0; padding: 0; font-weight: 500; font-family: 'Helvetica Neue', sans-serif; } main {} .bckgrnd { background-position: center center; background-repeat: no-repeat; background-attachment: ...

How to compare various values from two different Objects and then store them in an array-type variable

Below are two sets of data for objects: { "obj1": { "product": "Book", "category": "sci-fi", "title": "interstellar", }, "obj2": { & ...

What is the best way to make my text stand out against a faded background?

After mastering the basics of web development on Codecademy, I was eager to start my own blog and create a unique website. One challenge I encountered was figuring out how to fade the background without affecting the text. Despite researching various solut ...

The PHP file is outputting the JavaScript code but it is not executing

My goal is to develop an Edit Modal. With the necessary html code in place, I proceeded to write the following javascript/jquery script: <script type='text/javascript'> $(function() { <?php $q = $db->query("select * f ...

Attempted to display a Google Map within a lightbox, but encountered difficulties in getting it to

I've copied and pasted my code below. It may contain references to Google or Lightbox being undefined, but I'm unsure how to integrate them into the code provided. My goal is to display a Google map within a Lightbox popup, but so far I have been ...

What is the method for determining profit as a percentage?

I need some help with a basic question. I have two variables, 'a' and 'b'. Variable A represents the money I receive from a customer, while variable B represents the money I will pay to a carrier. For example, if I receive $1000 from a ...

Enhancing SOAP message attributes with node.js (node-soap)

Spent the entire day struggling with this issue, and I would greatly appreciate any assistance! I am currently developing an application that interacts with a 3rd party SOAP web service. The application is built using node.js and utilizes node-soap. Howev ...