HTTP DELETE/PUT запросы из HTML формы/JavaScript
можно ли вызвать HTTP DELETE/PUT запросы из HTML формы или JavaScript? это специфика модуля который я использую, и я тут не в силах изменить логику. Есть выход дергать прямо из UI либо писать прокси контроллер.
1 ответов
Вы можете делать такие запросы используя AJAX.
Например с помощью jQuery
http://api.jquery.com/jQuery.ajax/ (параметр type)
$.ajax({
url: "/some/url/",
type: "PUT",
data: ({id : 123456}),
success: function(msg){
alert(msg);
}
})
url: "/some/url/",
type: "PUT",
data: ({id : 123456}),
success: function(msg){
alert(msg);
}
})
Connector для Extjs, который я писал для своего RESTful приложения.
Ext.ns("SC.core");
SC.core.Connection = Ext.extend(Ext.data.Connection, {
defaultHeaders: {
"Accept": 'application/json'
},
method: "GET",
//
load: function(url, params, success, failure, scope) {
return this.req(url, params, success, failure, scope, "GET");
},
post: function(url, params, success, failure, scope) {
if (params)
params = Ext.util.JSON.encode(params);
return this.req(url, params, success, failure, scope, "POST", {"Content-Type":"application/json"});
},
update: function(url, params, success, failure, scope) {
if (params)
params = Ext.util.JSON.encode(params);
return this.req(url, params, success, failure, scope, "PUT", {"Content-Type":"application/json"});
},
del: function(url, params, success, failure, scope) {
return this.req(url, params, success, failure, scope, "DELETE");
},
upload: function(url, params, files, success, failure, scope) {
var f = document.createElement("form");
f.id = Ext.id();
for (var k in params) {
var el = document.createElement("input");
el.name = k;
el.value = Ext.util.JSON.encode(params[k]);
f.appendChild(el);
}
if (files) {
for (var k in files) {
if (Ext.isElement(files[k])) {
var nn = files[k].cloneNode(true);
nn.name = k;
f.appendChild(nn);
}
}
}
document.body.appendChild(f);
return this.request({
url: url,
method: "POST",
isUpload: true,
debugUploads: true, //DEBUG MODE ONLY !!!
success: this.resultSuccess.createDelegate(this, [success, scope], true),
failure: this.resultFailure.createDelegate(this, [failure, scope], true),
// params: params,
form: f
});
console.log(f);
},
//private
req: function(url, params, success, failure, scope, method, headers) {
return this.request({
url: url,
method: method,
headers: headers,
success: this.resultSuccess.createDelegate(this, [success, scope], true),
failure: this.resultFailure.createDelegate(this, [failure, scope], true),
params: params
});
},
//private
resultSuccess : function(response, options, cb, scope) {
if (cb) {
var res = null;
try {
res = Ext.util.JSON.decode(response.responseText);
} catch(e) {
console.warn("decoding response faild", response);
}
cb.call(scope || window, res, response.status, response);
}
},
handleFailure: function(response, e) {
if (response.status == RCODE.UNAUTHORIZED) {
Ext.Msg.confirm(i18n("error"), i18n("messages.invalidsession"), Ext.emptyFn);
return;
}
SC.core.Connection.superclass.handleFailure.apply(this, arguments);
},
resultFailure : function(response, options, cb, scope) {
if (cb) {
var res = null;
try {
res = Ext.util.JSON.decode(response.responseText);
} catch(e) {
//ignore
}
cb.call(scope || window, res, response.status, response);
}
}
});
SC.RPC = new SC.core.Connection();
SC.core.Connection = Ext.extend(Ext.data.Connection, {
defaultHeaders: {
"Accept": 'application/json'
},
method: "GET",
//
load: function(url, params, success, failure, scope) {
return this.req(url, params, success, failure, scope, "GET");
},
post: function(url, params, success, failure, scope) {
if (params)
params = Ext.util.JSON.encode(params);
return this.req(url, params, success, failure, scope, "POST", {"Content-Type":"application/json"});
},
update: function(url, params, success, failure, scope) {
if (params)
params = Ext.util.JSON.encode(params);
return this.req(url, params, success, failure, scope, "PUT", {"Content-Type":"application/json"});
},
del: function(url, params, success, failure, scope) {
return this.req(url, params, success, failure, scope, "DELETE");
},
upload: function(url, params, files, success, failure, scope) {
var f = document.createElement("form");
f.id = Ext.id();
for (var k in params) {
var el = document.createElement("input");
el.name = k;
el.value = Ext.util.JSON.encode(params[k]);
f.appendChild(el);
}
if (files) {
for (var k in files) {
if (Ext.isElement(files[k])) {
var nn = files[k].cloneNode(true);
nn.name = k;
f.appendChild(nn);
}
}
}
document.body.appendChild(f);
return this.request({
url: url,
method: "POST",
isUpload: true,
debugUploads: true, //DEBUG MODE ONLY !!!
success: this.resultSuccess.createDelegate(this, [success, scope], true),
failure: this.resultFailure.createDelegate(this, [failure, scope], true),
// params: params,
form: f
});
console.log(f);
},
//private
req: function(url, params, success, failure, scope, method, headers) {
return this.request({
url: url,
method: method,
headers: headers,
success: this.resultSuccess.createDelegate(this, [success, scope], true),
failure: this.resultFailure.createDelegate(this, [failure, scope], true),
params: params
});
},
//private
resultSuccess : function(response, options, cb, scope) {
if (cb) {
var res = null;
try {
res = Ext.util.JSON.decode(response.responseText);
} catch(e) {
console.warn("decoding response faild", response);
}
cb.call(scope || window, res, response.status, response);
}
},
handleFailure: function(response, e) {
if (response.status == RCODE.UNAUTHORIZED) {
Ext.Msg.confirm(i18n("error"), i18n("messages.invalidsession"), Ext.emptyFn);
return;
}
SC.core.Connection.superclass.handleFailure.apply(this, arguments);
},
resultFailure : function(response, options, cb, scope) {
if (cb) {
var res = null;
try {
res = Ext.util.JSON.decode(response.responseText);
} catch(e) {
//ignore
}
cb.call(scope || window, res, response.status, response);
}
}
});
SC.RPC = new SC.core.Connection();