سؤال

I am trying to do the following

$("select.languages").on("change", function(){
        var lang = $(this).find("option:selected").text();
        $.get(url: "/search",
                data: {"lang": lang},
            dataType: 'script');
    });

so every time a language is selected I would call /search with the language and by other SO answers to make dataType be "script"

but this doesn't quite work. What does work is

$.get( "/search.js",{"lang": lang} )

yet this pollutes the url since the format is very explicit and doing using this method requires me to add extra pointless code

can I implicitly set the response format?

هل كانت مفيدة؟

المحلول

Try this instead:

$.ajax({
  url: "/search",
  data: {"lang": lang},
  dataType: 'script'
});

The get shorthand method does not accept the parameters as an object like this.

نصائح أخرى

Probably a syntax error - I would try this:

   $("select.languages").on("change", function(){
        var lang = $(this).find("option:selected").text();
        $.ajax({
            url: "/search",
            data: {"lang": lang}
        });
    });

Since you're trying to send a JS request, you probably won't even need the dataType property, as Ajax expects it by default: $.ajax - dataType

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top