2

Using Laravel -

abort(403, 'This is an exception message.');

How can I access this message from my JavaScript?

// Get the project based on projectID.
$.ajax({
    type: 'GET',
    url: '/api/projects/getProject/' + projectID
}).done(function(response){
    ProjectStore.getProjectSuccessful(response);
}).error(function(error){
    console.log(error)
});

My error object has a statusText property of 'Forbidden', and a responseText property that contains the entire HTML (in which I can see my error message) - but is there an easy way to access that message from my error object without parsing through the HTML?

D'Arcy Rail-Ip
  • 11,505
  • 11
  • 42
  • 67
  • 1
    `Forbidden` is an http error, errorcode 403. So you actually get your error - check if you have the correct permissions – messerbill Sep 10 '15 at 15:38
  • My bad - I am using `abort(403, 'This is an exception message.')` - which is why it is showing 'Forbidden'. I'm still unsure of how to access the message itself without going through the HTML that is returned as response. – D'Arcy Rail-Ip Sep 10 '15 at 16:33

1 Answers1

2

I found that the abort() function takes a third argument - an array - and this gets sent up through the response header.

So - updated code in PHP:

$error = 'User not authorized to view project.'; 
abort(401, $error, array('errorText'=>$error));

In my JavaScript:

// Get the project based on projectID.
$.ajax({
    type: 'GET',
    url: '/api/projects/getProject/' + projectID
}).done(function(response){
    ProjectStore.getProjectSuccessful(response);
}).error(function(error){
    console.log(error.getResponseHeader('errorText'));
});
D'Arcy Rail-Ip
  • 11,505
  • 11
  • 42
  • 67