AngularJS $resource url causes “question mark” ? to appear with no params

爱⌒轻易说出口 提交于 2019-12-13 13:22:19

问题


I have a $resource that defines a custom url for the :all method.

angular.module('MyApp').
  factory('Object', ['$resource', ($resource) ->
    $resource(
      '/api/groups/:group_id/objects/:id.json',
      {
        id: '@id',
        group_id: '@group_id'
      },
      all: {
        method: 'GET',
        url: '/api/objects/all.json'
      }
    )
  ])

When my page loads, the request goes out to '/api/objects/all.json?'. It's loading correctly, but the presence of the ? is confusing to me. I didn't pass it any parameters, so why does angular add the ? to the request?

Can I get rid of it somehow?


回答1:


Since angular v.1.2.14 this issue has been fixed. Update to it and have a clear url without unnecessary question




回答2:


You should move your top level parameters into the method that will be expecting an id into the definition of a get method. Since you have { id: @id } where it is, the resource service is thinking you'll be adding an id and group id to the call.



来源:https://stackoverflow.com/questions/19575565/angularjs-resource-url-causes-question-mark-to-appear-with-no-params

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