[英]Appending service name in jQuery.ajax url parameter
If I was to set up future ajax calls using: 如果我要使用以下命令设置将来的ajax调用:
$.ajaxSetup({
url: '/WebServices/AjaxService.asmx',
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8'
});
Is there any way I could append the service name in future calls? 有什么办法可以在以后的呼叫中附加服务名称? Like this:
像这样:
$.ajax({
url: '+=/ServiceName'
});
Or is a global variable my best option? 还是全局变量是我的最佳选择?
Not the way you are suggesting (although $.ajaxSetup.url
could be used that way), but you may want to create an object that has the path fixed and you can set the service method name: 不是您建议的方式(尽管
$.ajaxSetup.url
可以那样使用),但是您可能想要创建一个固定路径的对象,并且可以设置服务方法名称:
function Service(){
var path = 'http://myserver';
this.getServiceUrl = function(serviceName) { return path + '/' + serviceName; };
}
...
var s = new Service();
$.ajax({
url: s.getServiceUrl('myServiceName'),
...
});
Or something like that. 或类似的东西。 I hope that helps.
希望对您有所帮助。
$.ajaxSettings
will allow you to access the ajax settings. $.ajaxSettings
将允许您访问ajax设置。 You could then go: 然后,您可以去:
$.ajax({
url: $.ajaxSettings + "/ServiceName"
...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.