开发者

Is there a better way to do this ajax call

$('#request_song').autocomplete({
  serviceUrl: '<%= ajax_path("trackName") %>',
  minChars:1,
  width: 300,
  delimiter: /(,|;)\s*/,
  deferRequestBy: 0, //miliseconds
  params: { artists: 'Yes' },
onSelect: function(value, data){
       artist = $('#request_artist').val(); //this will return "The Killers"
  开发者_运维知识库 //make an ajax request to "/events/artist"


},

I am trying to make an ajax request inside the onSelect function is there a better way and if not what would be the syntax for calling "/events/The killers" and what about the string replace in that i need. Is there a clean way of doing this


How do you call ajax via jQuery? Like the following...

onSelect: function(value, data){
       artist = $('#request_artist').val(); //this will return "The Killers"
  $.ajax({
    type: "POST",
    url: "/events/" + artist,
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      alert(msg);
    }
});
}

I would suggest either formatting the "Artist" variable server side so that autocomplete returns the artist in the correct format, alertanively format it with a regular expression like this...

url: "/events/" + formatName(artist),

...

function formatName(artist) {
  return artist.replace(/[\W]/, "")
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜