NetworkError:405方法不允许AngularJS REST

[英]NetworkError: 405 Method Not Allowed AngularJS REST


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

在AngularJS中,我有以下功能,它工作正常:

$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):

现在我想将它从一个文件更改为一个url(使用一些甜蜜的Laravel 4返回json):

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

$ 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?

有什么问题?是因为fruit.json是“本地”而localhost不是?

4 个解决方案

#1


6  

From w3:

从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?

它表示URL:http:// localhost / fruitapp / fruits服务器正在响应不允许使用GET方法。是POST还是PUT?

#2


1  

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

您使用的角度js版本将<= 1.2.9。

If Yes, try this.

如果是,请试试这个。

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

#3


0  

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:

我的SpringBoot项目遇到了类似的问题,我在浏览器控制台中遇到了同样的错误,但是当我查看后端日志时,我看到了一条不同的错误消息,它抛出了这个错误:“org.springframework.web。 HttpRequestMethodNotSupportedException,message =不支持请求方法“DELETE”“事实证明我错过了后端控制器中的{id}参数:

** 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;
}

#4


0  

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 *).

对我来说,没有为CORS配置服务器。以下是我在Azure上的使用方法:在Azure上启用CORS我希望类似的东西也适用于您的服务器。我还找到了如何在web.config上配置CORS的建议,但不能保证:在web.config中配置CORS。通常,您的服务器会有一个预检请求,如果您执行了跨域请求(来自您服务器之外的其他URL),则需要允许服务器上的所有源(Access-Control-Allow-Origin) *)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/04/04/44a6bf60ba049e50506c7d27fbe34ee8.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号