What is the method for setting the content-type in an AJAX request for Android browsers?

I am facing an issue with my ajax call to the Rails server. The response from the server varies between HTML and JSON based on the content type. Surprisingly, this works smoothly on iPhone and desktop browsers like Chrome, but I am encountering a problem on the Android browser. It seems that the server is not recognizing the content type correctly. I suspect that the overrideMimeType function is not working as expected!

Is there any alternate solution available? If I am unable to resolve this, I might have to create a separate URL to handle JSON requests.

The code snippet in question looks like this :

function makeAjaxCall {
    xmlhttp=new XMLHttpRequest();
    targetUrl = window.location.pathname;
    xmlhttp.open('GET',targetUrl,true);     
    xmlhttp.overrideMimeType("application/json");
    xmlhttp.onreadystatechange=function()
    {

      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
              // Issue: Response is received as HTML instead of JSON on Android browsers
        alert('response:' + xmlhttp.responseText);
        r = eval('(' + xmlhttp.responseText + ')');
                    // Handling additional operations
      }
    }
    xmlhttp.send();
}

Answer №1

Perhaps you also require

xmlhttp.setHeader("Content-Type", "application/json");

in addition?

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 can I add a character at a precise location while keeping the existing tags intact

Latest Update After further testing, it seems that the code performs well with a faux spacer, but runs into issues with the regex. The following scenarios work correctly: Selecting words above or below the a tag Selecting just one line directly above or ...

AngularJS ng-repeat: displaying a list of filtered outcomes exclusively

I currently have a ng repeat that loops through a set of results. <a class="list-group-item" href="#trip/{{trip.id}}/overview" ng-repeat="trip in trips | filter:search | limitTo:-15"> Basically, as I enter more text into my input field, the list sh ...

Is there a way to retrieve the previous value in an input field's onChange event?

I am working with inputs in a React project and have assigned a function to their onChange event. While I have been able to access the current value, I am now looking for a way to retrieve the previous value as well. The reason I need the old value is bec ...

"Implementing a loading function on a particular div element within a loop using

Hey everyone! I'm new to this forum and have recently made the switch from jQuery to Vue.js - what a game-changer! However, I've hit a little snag. I need to set v-loading on multiple buttons in a loop and have it start showing when clicked. Her ...

Using React, implement nested fetch calls to make multiple AJAX requests

Upon executing a function, it retrieves all zones in an environment and then proceeds to make an API call for the border points of each zone. While all fetches are successfully executed, there's a problem arise in the then block for the initial fetch ...

Loading JavaScript on a different page using AJAX is not possible

Why is it that AJAX can load HTML, CSS, PHP, etc., but not JavaScript files when using JavaScript? Does AJAX have limitations in this regard? If so, how would one go about loading another HTML page that contains JavaScript with AJAX? Here's a simple ...

Tips for ensuring the drop down button remains selected

My goal is to keep the sorting drop-down button selected after clicking on it, instead of resetting back to "All". Below are my HTML, CSS, and jQuery code. You can view the functionality on my website here: jQuery/Javascript: $(document).ready(function($ ...

What is preventing me from accessing React state within Tracker.Autorun?

I'm feeling lost on this one. I have implemented a Tracker.autorun function to monitor when my Mongo subscription is ready for querying (following the advice given in this previous Meteor subscribe callback). It seems to be working fine as it triggers ...

I'm having trouble getting the delete button to function properly in Laravel. Since I'm new to this framework, I'm unsure of how to fix

Below is the jQuery script I am using: $(document).on('click', '.btnDeleteStudent', function () { var id = $(this).val(); var token = $("meta[name='csrf-token']").attr("content"); ...

Error: Unable to locate package @babel/preset-vue version 7.1.0

I am currently working on a simple website using Ruby on Rails and Vue.js, but I am running into issues when trying to start the local server. After executing npm run dev in the terminal, I encountered 2 errors: This dependency was not found: * /Users/mu ...

Automatically inserting a row into an HTML table

My table structure looks like this: <form name="frm_data_nasabah2" method="post" enctype="application/x-www-form-urlencoded" action="<?php echo $page_action;?>"> <table class="table table-bordered table-hover" id="tab_logic_ ...

What is the best way to retrieve a property with a period in the method name in JavaScript?

One dilemma I'm facing is trying to access the tree.removenode method through chartContext in Angular. It's been a challenge for me to understand how to achieve this. https://i.stack.imgur.com/yG7uB.png ...

Display a jQuery loading window

Need help with displaying a loading div when making an ajax post request. I've tried the following code but it's not working. Can someone please assist in resolving this issue? $(document).ready(function() { $('a.pagerlink').click( ...

Guide on integrating a php script into a react application

I'm currently in the process of setting up a functional contact form on my website by following the guidance provided in this article link. However, the tutorial includes a php script to manage the post data, and I am using create-react-app and unsure ...

modify brand information through the use of javascript and customizable settings

In the default setting, I want all product details to be displayed. If the option value matches the product's brand name (b_name), then I want to display the corresponding details. Let's consider a list of products with their details: Samsung ...

Unable to retrieve data from the database within PHP code

I have successfully built a shopping cart website utilizing SQL, HTML, and PHP. Below is the code snippet for the 'Add to Cart' button: <form method="post" action="cart.php" class="form-inline"> <input type="hidden" value="&apos ...

What is the best way to retrieve the value of a button using javascript?

After trying to extract the value from a button in JavaScript, here is the code I used: for(i=0; i<cars.length;i++){ var p = `<button id="myBtn" onclick="myFunction()" value="${cars[i]}">${cars[i]}</button>` func ...

Display the div only after all images have finished loading in response to the AJAX call

Prior to diving into this, I am determined to steer clear of utilizing Jquery for personal reasons that I won't delve into. So please refrain from suggesting it, as that is not the solution I seek. I am currently working on a web page that sends mult ...

Exploring the world of JSON manipulation in JavaScript

I've been experimenting with JSON recently and came across something I don't quite understand. Here is an example of some code I used: var str = "{'name':'vvv'}"; var cjson = eval ("(" + str + ")"); alert( ...

`local data erasing in Ionic app on both iOS and Android devices`

I am in the process of developing a mobile application with a backend server built on Rails. The main concept is that upon successful user sign-in, the server sends back a unique token along with their user_id. These two pieces of information are stored in ...