Occasionally, the Axios ajax request may return HTML as a response

While working with Vue.js 2, I encountered an issue with my ajax call using axios. Everything was working fine until I navigated to another page and returned to the page with the ajax call, where it suddenly started returning html instead of json objects.

I'm puzzled by this behavior. When I tested the URL in Postman, I received json data without any issues.

It's worth mentioning that my back-end is built with Laravel 5.5.

Here is the HTML response I received:

   <!DOCTYPE html>\n<!--[if IEMobile]><html class=\"no-js iemobile\" lang=\"nl\"><![endif]-->\n<!--[if lt IE 7 ]><html class=\"no-js ie6\" lang=\"nl\"><![endif]-->\n<!--[if IE 7 ]><html class=\"no-js ie7\" lang=\"nl\"><![endif]-->\n<!--[if IE 8 ]><html class=\"no-js ie8\" lang=\"nl\"><![endif]-->\n<!--[if IE 9 ]><html class=\"no-js ie9\" lang=\"nl\"><![endif]-->\n<!--[if (gte IE 9)|!(IE)]><!--><html class=\"no-js\" lang=\"nl\"><!--<![endif]-->\n<head>\n\t<!-- Google Tag Manager -->\n<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':\n        new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\n        j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\n        'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);\n    })(window,document,'script','dataLayer','GTM-TP9MVZN');</script>\n<!-- End Google Tag Manager -->\n    <meta data-required=\"true\" http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n\n<title>Televisions </title>\n\n<meta d…", status: 200, statusText: "OK", headers: {…}, config: {…}, request: XMLHttpRequest }

My Axios call in Vue.js code is as follows:

Axios.get(url, { params: params }).then((response) => {
           console.log(response);
});

In my app.js file, I have set the header using the following code:

Axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

Any insights or suggestions on resolving this issue would be greatly appreciated. Thank you.

Answer №1

Encountered a similar problem and successfully resolved it. Rails caches the page to minimize the number of requests when navigating back.

To deactivate this feature, refer to this helpful post

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

Organize divs based on their attributes

Using inspiration from this SO post, my goal is to group divs based on their "message-id" attribute. The idea is to wrap all divs with the same "message-id" in a div with the class name "group". <div class="message" message-id="1"> ...

Icons positioned absolutely outside of components will not be responsive to user interactions

How can I ensure that the icon on my tab bar can be clicked only when it covers a specific part of the screen, and not when it is in other areas? Any suggestions on how to fix this? Image Below is the code for the tab bar: <View style={{ background ...

Ordering ng-repeat in AngularJS using a separate arrayDiscover how to easily order your

Imagine I have an array containing keys in a specific order orderedItems=["apple","banana","orange]; and there is a JSON object that I want to display using ng-repeat but following the sequence specified in the array: {"fruits": { "apple":{ ...

Problem with bcrypt npm installation on Mac OS X 10.9 and Node v0.10.22

Operating System Information: Mac OS X 10.9 Node version: v0.10.22 An error occurs when attempting to install the bcrypt package. Any suggestions on how to resolve this issue? Any assistance would be highly appreciated. > [email protected] install /U ...

AngularJS: Enhancing User Experience by Preserving View State and Setup through Routes

Is it possible in a single page application to switch back and forth between AngularJS routes while maintaining the same state? Traditionally, this would involve binding data in a parent scope. However, for views with extensive graphical elements, this me ...

"Troubleshoot: jQuery UI accordion not functioning properly in response to

I've encountered an issue with the accordion functionality while working with dynamic data. To address this, I'm incorporating JavaScript to generate <div> and <h3> tags for the accordion. Here is the code snippet I am using: $(&ap ...

What happens when the `.push` command runs the "Undefined" function in jQuery - is it possible to execute a number instead?

My script is quite simple - I'm trying to use the push method to add the result of a function called dNumber into the variable named pattern. However, every time I try this, the result is always "undefined". Oddly enough, when I try pushing just plain ...

Using the "bind()" method within a routed component

Here is a simplified Angular 1.5.x component example that I've set up in a jsfiddle: appModule.component('mainComponent', { controller: function() { var x = 0; this.broadcast = function() { this.onUpdate({ count: x++ ...

Strategies for ensuring a promise is fulfilled before moving on to the next iteration in a never-ending for loop in JavaScript

I've been exploring ways to ensure that a promise is resolved before moving on to the next iteration in a for loop. One suggestion was to use the setInterval() function instead of a for loop, but this isn't ideal since it's hard to predict w ...

What is preventing the addition of links to the navigation bar when using a sticky navigation bar?

Currently, I am in the process of developing a blog website using Django. One of the features I have successfully implemented is a sticky navigation bar. However, I am facing a challenge with adding links to the menu on the navigation bar due to existing ...

Solving templateUrl resolution of directives in Angular with RequireJS

Currently, I am using AngularJS and RequireJS in my single-page application (SPA). To organize imports efficiently, I rely on RequireJS. With RequireJS, I can utilize features like baseUrl to resolve import paths seamlessly. Now, I wish to apply the same r ...

Saving the current language (i18n) in local storage using VueJS

I'm working on a Vue app that utilizes Vue Routers and the Vue $i18n plugin. Here's how I've structured my HTML: <div class="locale-changer"> <select v-model="this.$i18n.locale" class="btn"> ...

Looking to remove an item from an array using Redux?

Is there an issue with my reducers? Here is how they are set up: export default (itemsList = [], action) => { if (action.type === 'ADD_ITEM') { return [...itemsList, action.payload] } return itemList } The deleting reduce ...

implementing a SetTimeOut function following the clicking of a button

Hey there, I've been working on a code snippet that switches the display state from block to none with an onClick function. However, I'm looking to add a delay before the state change happens so that there's time for an animation effect to ...

Simulating a service call in an AngularJS controller

Here is the code for my Controller: (function () { 'use strict'; angular.module('myApp').controller('myCtrl', function ($scope, myService) { // Start -----> Service call: Get Initial Data myService ...

Tips for transferring an entire array to a servlet and retrieving it efficiently

I have been attempting to collect all jqgrid data into one array and send it to a servlet. So far, I have tried the following code snippet: var rows= jQuery("#list").jqGrid('getRowData'); var paras=new Arr ...

Having trouble accessing undefined properties in ReactJs? Specifically, encountering issues when trying to read the property "name"?

I am facing an issue with using the data in my console even though I can log it. The structure of my data object is as follows: {_id: '616bf82d16a2951e53f10da4', name: 'abd', email: '[email protected]', phone: '123456789 ...

"The issue persists where the Vuex action fails to update the state every time it is called from

Struggling with filtering my blog posts using Vuex. I update the data based on the category name extracted from the URL on route change and pass it as the action payload. This logic is implemented within a method called in 'mounted'. The computed ...

A step-by-step guide on integrating a Django template tag and HTML element into an HTML page using Javascript

Is there a safe way to insert HTML that includes Django template tags using JavaScript in my project? For instance, if my template contains the following code: <div id="some-element"> <span id="my-tag">{{ my_data }}</sp ...

[Vue alert]: The event "lazyLoadError" was triggered with an undefined handler

Currently, I am utilizing the combination of vue js and laravel. To enhance my project, I decided to incorporate a Vue component for Slick-carousel, but encountered the following error: [Vue warn]: Invalid handler for event "lazyLoadError": got unde ...