Angular $resource doesn't change method

别来无恙 提交于 2020-01-24 19:46:26

问题


I'm trying to configure a simple service with Angular.js and Couchdb

var App = angular.module('myapp', ['ngResource'], function(){});

App.factory('products', ['$resource', function($resource) { 

  var Products = $resource(
      'http://localhost\\:5984/products/_all_docs',
      {},
      { all: { method: 'GET'} }
  );

  return {
    all: function() {
        return Products.all();
    }
  };
}]);

When I call products.all() from my controller always I get

Request headers

OPTIONS /productos/_all_docs HTTP/1.1
Host: localhost:5984
Connection: keep-alive
Access-Control-Request-Method: GET
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17
Access-Control-Request-Headers: accept, origin, x-requested-with
Accept: */*
DNT: 1
Referer: http://localhost:8000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-419,es;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

Response headers

HTTP/1.1 405 Method Not Allowed
Server: CouchDB/1.2.1 (Erlang OTP/R14B04)
Date: Tue, 29 Jan 2013 22:15:31 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 69
Cache-Control: must-revalidate
Allow: GET,HEAD,POST

Request URL:http://localhost:5984/productos/_all_docs
Request Method:OPTIONS
Status Code:405 Method Not Allowed

I can not imagine why it sends an OPTIONS method when I haven't declared that!


回答1:


The OPTIONS method comes from doing a cross origin request. See Why am I getting an OPTIONS request instead of a GET request?. Even though both the host and the request is to localhost, the ports are different (5984, 8000). See Same origin host, different ports in JS.



来源:https://stackoverflow.com/questions/14593130/angular-resource-doesnt-change-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!