简体   繁体   English

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 );

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不是?

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. 它表示URL: http://localhost/fruitapp/fruits服务器正在响应不允许使用GET方法。 Is it a POST or PUT ? POST还是PUT

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' 
    }
});

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

For me, it was the server not being configured for CORS. 对我来说,没有为CORS配置服务器。 Here is how I did it on Azure: CORS enabling on Azure I hope something similar works with your server, too. 以下是我在Azure上的工作方式: 在Azure启用CORS我希望类似的东西也适用于您的服务器。 I also found a proposal how to configure CORS on the web.config, but no guarantee: configure CORS in the web.config . 我还找到了如何在web.config上配置CORS的提议,但不能保证: 在web.config中配置CORS 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 *). 通常,您的服务器会有一个预检请求,如果您执行了跨域请求(来自您服务器之外的其他URL),则需要允许服务器上的所有源(Access-Control-Allow-Origin) *)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM