1

Axios 'get' request returns an empty object response when I pass the route to retrieve JSON object

I want to retrieve JSON data from my registration controller but gets an empty response

this is my registration controller

public function show($id)
{
    $registration =
         $this->registrationRepository->findRegistrationById($id);

    return response()->json($registration);
}

this is my script

 export default {
   props:{
      studentid:Number,
      registrationid:Number,
   },
   data(){
     return {
       registration:{}
     }
   },
   created(){
     this.loadRegisteredSubjects();
   },
   methods: {
     loadRegisteredSubjects(){
       axios.get('/admin/registrations/'+this.registrationid+'/show')
         .then(response => (this.registration = response)
     }
   }

 }

when I passed the route from where to retrieve the data

http://127.0.0.1:8000/admin/registrations/13/show

i get the desire results which is

{"id":13,"student_id":2,"subjects":"3,4,5","created_at":"2019-10-01 01:13:13","updated_at":"2019-10-01 01:13:13"}

unfortunately, I get an empty registration object when I pass the same route to Axios

4
  • 1
    In your browser's network panel, do you see a response? What's the HTTP code for that response? Commented Oct 1, 2019 at 23:55
  • yes, this is the response {"id":12,"student_id":1,"subjects":"1,2,3","created_at":"2019-10-01 00:29:33","updated_at":"2019-10-01 00:29:33"} Commented Oct 1, 2019 at 23:56
  • 1
    You are not using reponse, if so this response.data would error. Can you update the question with the actual code you are using? Commented Oct 2, 2019 at 0:06
  • 1
    Your latest edit still has a typo that will cause a syntax error. As I said below in another comment, you want .then(response => { this.registration = response.data }) Commented Oct 2, 2019 at 2:10

1 Answer 1

2

If the code you've shared is your actual code, I think you may just have a typo.

reponse => (this.registration = response.data)

reponse != response

Sign up to request clarification or add additional context in comments.

8 Comments

yes i have fix it but still get empty registration object even though i get a response in my browsers network panel
Time to start console.logging out things like response.data to make sure they contain what you think they contain, then.
({response}) => (this.registration = response.data)
One step at a time. Does what I suggested work? Do you get the response in the console?
@eons please update the code in your question if it's still not working. You want .then(response => { this.registration = response.data })
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.