Skip to content

CORS issue when responding with error #64

@gerob

Description

@gerob

Using Angular to create a SPA, it works fine as long as my API isn't responding with an error. Take for example this code:

try {
     return User::findOrFail($user_id);
} catch (ModelNotFoundException $e) {
     throw new ResourceException("No user record found.");
}

If the user is found, Angular successfully retrieves the data. However if the record does not exist then this error is thrown.

XMLHttpRequest cannot load http://api.example.com/users/. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://my-angular-site.com' is therefore not allowed access. 

Successful requests have these headers:

Access-Control-Allow-Origin →*
Cache-Control →no-cache, private
Connection →keep-alive
Content-Type →application/json
Server →nginx
Transfer-Encoding →chunked
X-Frame-Options →SAMEORIGIN
date →Fri, 23 May 2014 13:27:26 GMT

Responses provided by throwing an error have these headers:

Cache-Control →no-cache, private
Connection →keep-alive
Content-Type →application/json
Server →nginx
Transfer-Encoding →chunked
X-Frame-Options →SAMEORIGIN
date →Fri, 23 May 2014 13:31:29 GMT

UPDATE:

If I use the following code then returning the errors works.

try {
     return User::findOrFail($user_id);
} catch (ModelNotFoundException $e) {
     header('Access-Control-Allow-Origin: *');
     throw new ResourceException("No user record found.");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions