Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did not attempt to load JSON data because the request Content-Type was not 'application/json'. Axios

I'm doing a get method, when i run the code i'm getting this Did not attempt to load JSON data because the request Content-Type was not 'application/json'.. I have tried to set headers. Here is my code.

<template>
   <div class="container">
     <button @click="SearchTown"> Search Town </button>

   </div>
</template>

<script>
import axios from 'axios';
export default {
        name: 'SearchTown',
        props: {
            messege: String
        },
  data(){
    return{
      search: [],
    }
    
    },

  methods :{
      SearchTown() {
    axios
      .get('https://david.darwinist.io/proxy/5000/town/',{headers:{'content-type':'application/json'}})
     .then((response) => {// checking response in the console
            console.log(response.data)})
      .catch((error)=>{
        console.log(error)
      })
  }
 }
}

</script>

I'm having 400 error code. I need help.

This is my backend code

def getTown(session, town_dict):
    try:
        town = (
            session.query(Town)
            .join(Town.county)
            .join(County.nation)
            .where(County.name == town_dict["county"])
            .where(Town.name == town_dict["name"])
            ).one()
        town_county = copy.copy(town).__dict__
        del town_county["_sa_instance_state"]
        town_county["county"] = town.county.name
        town_county["nation"] = town.county.nation.name
        return town_county
    except MultipleResultsFound:
        return "bad Gateway!", 502
    except NoResultFound:
        return "Results not found!", 404

I'm not really sure if i have to change my query. Kindly advice.

like image 810
David Avatar asked Oct 23 '25 23:10

David


1 Answers

As commented in this GitHub issue:

https://github.com/axios/axios/issues/86#issuecomment-136505548

https://github.com/axios/axios/issues/86#issuecomment-139638284

Your GET request must have some data passed along it. data: {} should do the trick.

Some people find it kind of misleading. The explanation for this behaviour is here:

Content-Type describes what format the request data is in. If there is no request data, there is no need to specify the Content-Type.

like image 135
S. Dre Avatar answered Oct 25 '25 21:10

S. Dre