【问题标题】:Having problems appending api key to url in Backbone在 Backbone 中将 api 密钥附加到 url 时出现问题
【发布时间】:2013-10-02 00:04:37
【问题描述】:

如何将 ?apiKey=12341234 附加到每个 ajax 请求以进行获取、创建、保存等... MongoLabs api 需要这个,我一直在努力让它充分发挥作用。

我目前正在通过骨干教程截屏工作,作者正在使用 php 框架为骨干应用程序创建 api。我以为我会通过使用 MongoLabs 免费休息 api 变得更聪明......然后我花了 2 天时间阅读文档并试图让它发挥作用。

我最接近的是在调用 fetch 时传入参数(不适用于 create):Fetch Backbone collection with search parameters

{ data: $.param({'apiKey':'50b28f80e4b0995a3f4312a0'}) }

工作网址:
https://api.mongolab.com/api/1/databases/bbone_rest/collections/tasks/2/?q=&apiKey=50b28f80e4b0995a3f4312a0

jsfiddle 上的代码: http://jsfiddle.net/py4Pv/

# Encapsulate classes
window.App =
  apiKey: { data: $.param({'apiKey':'50b28f80e4b0995a3f4312a0'}) }
  Models: {}
  Views: {}
  Collections: {}



# The model for a single task
class App.Models.Task extends Backbone.Model
  #override backbones 'id' attr to match mongo's '_id' attr
  idAttribute: "_id"
  defaults:
    title: "None provided"
    completed: 0


# The model for a collection of tasks
class App.Collections.Tasks extends Backbone.Collection
  model: App.Models.Task
  url: 'https://api.mongolab.com/api/1/databases/bbone_rest/collections/tasks/'





##################### Views, prob not important ########################  


# View for a list of tasks
class App.Views.Tasks extends Backbone.View
  tagName: 'ul'

  initialize : () ->
    @collection.on 'add', @addOne

  render: ->
    # Cycle through each task in collection and call @addOne
    @collection.each @addOne
    return this

  addOne: (task) =>
    task = new App.Views.Task({ model: task })
    @$el.append( task.render().el )


# View for a single task unit
class App.Views.Task extends Backbone.View
  tagName: 'li'
  render: ->
    @$el.html( this.model.get('title'))
    return this

* 编辑 * 添加时的标头信息:$.ajaxSetup({ data: { key: "value"} after a task.destroy()

Request URL:https://api.mongolab.com/api/1/databases/bbone_rest/collections/tasks/1
Request Method:OPTIONS
Status Code:400 Bad Request

Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:origin, accept
Access-Control-Request-Method:DELETE
Connection:keep-alive
Host:api.mongolab.com
Origin:null
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11

Response Headersview source
Connection:close
Content-Length:50
Content-Type:application/json;charset=utf-8
Date:Fri, 30 Nov 2012 06:42:57 GMT
Server:Apache/2.2.22 (Ubuntu)

【问题讨论】:

    标签: javascript backbone.js


    【解决方案1】:

    You can specify a function for the url property of the model/collection -- 这样你每次都应该能够在查询字符串中附加 api。即,类似(抱歉,我不确定 Mootools 语法):

    url: () => 
       return 'https://api.mongolab.com/api/1/databases/bbone_rest/collections/tasks/' + this._id + '?api=' + App.apiKey
    

    【讨论】:

    • 这很接近,但如果我把它放在模型中,它会被集合 url 覆盖,如果我删除集合 url,那么 tasks.fetch() 会抱怨缺少集合 url。如果我把它放在集合中,那么它有一个未定义的模型 id:api.mongolab.com/api/1/databases/bbone_rest/collections/tasks/…
    • @SkinnyGeek1010 也许我遗漏了一些东西,但似乎您应该能够将 URL 函数同时放入集合和模型中。 fetch 之类的集合方法应该使用集合 URL,savedestroy 之类的模型集合也是如此……不是吗?
    • 你可以,但它不会自动为你创建一个ID。如果您为每个实例手动创建一个 id,则可以在 url 字符串中使用 get('_id') 。我已经放弃了决定使用 node/express/mongodb 滚动我自己的 api 哈哈。
    【解决方案2】:

    你试过了吗:

    $.ajaxSetup({
      data: {
        key: "value"
      }
    

    这会将关键参数添加到每个 ajax 请求。在 ajaxSetup 中声明数据是累积的。

    【讨论】:

    • 我尝试在文件的开头和结尾添加它,但没有成功。 (使用task.destroy)。它在客户端工作,但不附加 url。 (我在上面添加了标题信息)
    • edit,它确实适用于 fetch(允许我在 fetch 时省略传递 api 密钥)。但它仍然不适用于 .destroy()...不知道为什么。
    【解决方案3】:

    处理这个问题的最佳方法是 ajaxPrefilter

    var api_key = '465456132456'
    $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
      options.url = 'YOUR_URL' + options.url + '?key=' + api_key;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多