简体   繁体   中英

Appending service name in jQuery.ajax url parameter

If I was to set up future ajax calls using:

$.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:

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. You could then go:

$.ajax({ 
  url: $.ajaxSettings + "/ServiceName"
  ... 
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM