Ben
Ben

Reputation: 16649

NetworkError: 405 Method Not Allowed AngularJS REST

In AngularJS, I had the following function, which worked fine:

$http.get( "fruits.json" ).success( $scope.handleLoaded );

Now I would like to change this from a file to a url (that returns json using some sweet Laravel 4):

$http.get( "http://localhost/fruitapp/fruits").success( $scope.handleLoaded );

The error I get is:

"NetworkError: 405 Method Not Allowed - http://localhost/fruitapp/fruits"

What's the problem? Is it because fruit.json was "local" and localhost is not?

Upvotes: 12

Views: 51473

Answers (4)

seawave_23
seawave_23

Reputation: 1248

For me, it was the server not being configured for CORS. Here is how I did it on Azure: CORS enabling on Azure I hope something similar works with your server, too. I also found a proposal how to configure CORS on the web.config, but no guarantee: configure CORS in the web.config. In general, there is a preflight request to your server, and if you did a cross-origin request (that is from another url than your server has), you need to allow all origins on your server (Access-Control-Allow-Origin *).

Upvotes: 0

Arar
Arar

Reputation: 2066

I had a similar issue with my SpringBoot project, I was getting the same error in the browser console but I saw a different error message when I looked at the back-end log, It was throwing this error: "org.springframework.web.HttpRequestMethodNotSupportedException, message=Request method 'DELETE' not supported " It turned out that I was missing the {id} parameter in the back-end controller:

** Wrong code :** 
@RequestMapping(value="books",method=RequestMethod.DELETE)
	public Book delete(@PathVariable long id){
	Book deletedBook = bookRepository.findOne(id);
	bookRepository.delete(id);
	return deletedBook;
}

** Correct code :** 

@RequestMapping(value="books/{id}",method=RequestMethod.DELETE)
	public Book delete(@PathVariable long id){
		Book deletedBook = bookRepository.findOne(id);
		bookRepository.delete(id);
		return deletedBook;
}

Upvotes: 0

Vikas Salvi
Vikas Salvi

Reputation: 11

The angular js version you are using would be <= 1.2.9.

If Yes, try this.

return $http({                
    url: 'http://localhost/fruitapp/fruits',
    method: "GET",
    headers: {
        'Content-Type': 'application/json', 
        'Accept': 'application/json' 
    }
});

Upvotes: 1

Xesued
Xesued

Reputation: 4167

From w3:

10.4.6 405 Method Not Allowed

The method specified in the Request-Line is not allowed for the resource 
identified by the Request-URI. The response MUST include an Allow header 
containing a list of valid methods for the requested resource.

It means the for the URL: http://localhost/fruitapp/fruits The server is responding that the GET method isn't allowed. Is it a POST or PUT?

Upvotes: 6

Related Questions