本文介绍了如何指定URL和头骨干在我的模型中使用的方法CRUD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要作出格外API密钥的需要服务器的请求,我需要使用CRUD方法涂更新我的模型,并尽快...

i need to make request on server that needs of particulary api key and i need to use the crud method tu update my model and as soon as...

例如我在阿贾克斯这code从服务器获取元素:

For example i have this code in ajax to get element from server:

 function getapi() {

$.ajax({
    url: 'https://api.parse.com/1/classes/autolavaggi/QSfl*****',
    type: 'GET',
    dataType: 'json',

    success: function(obj) { 

        alert("nome autolavaggio "+obj.nome);

    },
    error: function() {
        alert('Errore');


    },
    beforeSend: setHeader
});
}  

    //GET GET  GET  GET  GET GET  GET  GET  Header Header Header Header
    function setHeader(xhr) {
xhr.setRequestHeader('X-Parse-Application-Id', 'aqLJlmE2rRXBOy***************');
xhr.setRequestHeader('X-Parse-REST-API-Key', 'gvT2Isd5vAvjgq*****************');
}

我怎样才能做到分配这个特殊的Ajax调用CRUD方法保存,提取或另一种??

How can i do to assign this particular ajax call to crud method save,fetch or another??

推荐答案

每个的CRUD方法接受一个选项哈希值,将被转发到Ajax调用。在集合的情况下获取:

Each of the crud methods accept an options hash that will get forwarded to the ajax call. In the case of a collection fetch:

var Model = Backbone.Model.extend({});
var Collection = Backbone.Collection.extend({
  model: Model,
  url: 'https://api.parse.com/1/classes/autolavaggi/QSfl*****'
});

var setHeader = function (xhr) {
  xhr.setRequestHeader('X-Parse-Application-Id', 'aqLJlmE2rRXBOy***************');
  xhr.setRequestHeader('X-Parse-REST-API-Key', 'gvT2Isd5vAvjgq*****************');
}

var collection = new Collection();
collection.fetch({ beforeSend: setHeader });

另外,覆盖的同步:

Alternatively, override sync:

var sync = Backbone.sync;
Backbone.sync = function(method, model, options) {
  options.beforeSend = function (xhr) {
    xhr.setRequestHeader('X-Parse-Application-Id', 'aqLJlmE2rRXBOy***************');
    xhr.setRequestHeader('X-Parse-REST-API-Key', 'gvT2Isd5vAvjgq*****************');
  };

  // Update other options here.

  sync(method, model, options);
};

这篇关于如何指定URL和头骨干在我的模型中使用的方法CRUD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:34