admin管理员组文章数量:1435859
I am creating a application where I have lot of ajax calls to a remote server and use them extensively. As the code is almost same in all calls, I want to create a new function which I can reuse. I am struck up in defining the parameter structure for the "data" parameter. I will explain below my problem.
Sample of my current ajax call is provided below.
Current Call Sample:
$.ajax({
beforeSend: function() {
$.mobile.loading('show');
},
plete: function() {
$.mobile.loading('hide');
},
type: 'GET',
url: 'http://localhost/test-url/',
crossDomain: true,
data: {appkey: '1234567', action: 'action1','name':'me'},
dataType: 'jsonp',
contentType: "application/javascript",
jsonp: 'callback',
jsonpCallback: 'mycallback',
async: false,
error: function() {
//some operations
},
success: function(data) {
//some operations
}
});
The re-usable function that I have created:
function newAjax(parm, successCallback, errorCallback) {
$.ajax({
beforeSend: function() {
$.mobile.loading('show');
},
plete: function() {
$.mobile.loading('hide');
},
type: 'GET',
url: 'http://localhost/test-url',
crossDomain: true,
data: {appkey: '1234567', parm: parm},
dataType: 'jsonp',
contentType: "application/javascript",
jsonp: 'callback',
jsonpCallback: 'mycallback',
async: false,
success: function() {
successCallback();
},
error: function() {
errorCallback();
}
});
}
Question:
I will be passing the the parameters for the ajax call via "parm" parameter. I want the data value to be directly added to the parent "data" parameter. And not as a sub-object of data. The appKey remains same across all calls and so I keep it in the actual function.
I want both the success and error callback functions to be optional. If not provided they should be ignored.
I am creating a application where I have lot of ajax calls to a remote server and use them extensively. As the code is almost same in all calls, I want to create a new function which I can reuse. I am struck up in defining the parameter structure for the "data" parameter. I will explain below my problem.
Sample of my current ajax call is provided below.
Current Call Sample:
$.ajax({
beforeSend: function() {
$.mobile.loading('show');
},
plete: function() {
$.mobile.loading('hide');
},
type: 'GET',
url: 'http://localhost/test-url/',
crossDomain: true,
data: {appkey: '1234567', action: 'action1','name':'me'},
dataType: 'jsonp',
contentType: "application/javascript",
jsonp: 'callback',
jsonpCallback: 'mycallback',
async: false,
error: function() {
//some operations
},
success: function(data) {
//some operations
}
});
The re-usable function that I have created:
function newAjax(parm, successCallback, errorCallback) {
$.ajax({
beforeSend: function() {
$.mobile.loading('show');
},
plete: function() {
$.mobile.loading('hide');
},
type: 'GET',
url: 'http://localhost/test-url',
crossDomain: true,
data: {appkey: '1234567', parm: parm},
dataType: 'jsonp',
contentType: "application/javascript",
jsonp: 'callback',
jsonpCallback: 'mycallback',
async: false,
success: function() {
successCallback();
},
error: function() {
errorCallback();
}
});
}
Question:
I will be passing the the parameters for the ajax call via "parm" parameter. I want the data value to be directly added to the parent "data" parameter. And not as a sub-object of data. The appKey remains same across all calls and so I keep it in the actual function.
I want both the success and error callback functions to be optional. If not provided they should be ignored.
-
just a note,
contentType: "application/javascript",
doesn't make sense (you're sending GET params to the server, not javascript...), andcrossDomain: true,
is almost never needed. Also, since you're properly using callbacks,async: false
isn't needed. Lastly, since you're using jsonp, none of the aforementioned parameters are used anyway. – Kevin B Commented Nov 11, 2013 at 16:43 -
You need to look at
extend()
... api.jquery./jQuery.extend – Reinstate Monica Cellio Commented Nov 11, 2013 at 16:45 - 3 Why reinvent the wheel? jQuery.ajaxSetup – epascarello Commented Nov 11, 2013 at 16:45
- It looks like you're just throwing everything you can think of into the request without understanding what the parameters do. – Kevin B Commented Nov 11, 2013 at 16:48
- The request does a cross-domain call so I added those parameters. It uses jsonp, so I read somewhere to use the contentType.. I am learning all these stuffs.. so excuse me for my errors :) – Purus Commented Nov 11, 2013 at 16:54
1 Answer
Reset to default 5You can use the
jQuery.extend
method to bine two or more objects together.data: jQuery.extend({appkey: '1234567'}, parm),
You can check that you were actually passed functions for
successCallback
anderrorCallback
usingtypeof var === 'function'
;success: function () { if (typeof successCallback === 'function') { successCallback(); } }, error: function () { if (typeof errorCallback === 'function') { errorCallback(); } }
... although it might be nicer if you just returned the Promise created by the AJAX request, and let the caller add their success, error handlers if they wanted;
function newAjax(parm) { return jQuery.ajax({ /* as before, but without success and error defined */ }); }
... then:
newAjax().done(function () { // Handle done case }).fail(function () { // Handle error case. });
If a caller doesn't want to add an error handler, they just don't call
fail()
;newAjax().done(function () { // Handle done case });
本文标签: javascriptGeneric ajax() call function for resusabilityStack Overflow
版权声明:本文标题:javascript - Generic $.ajax() call function for resusability - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745673918a2669734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论