开发者

can the url: contain a variable in a jquery ajax call

开发者 https://www.devze.com 2023-01-11 22:47 出处:网络
I have开发者_开发知识库 the following Jquery ajax call but would like to put as the Url a variable instead of a string.

I have开发者_开发知识库 the following Jquery ajax call but would like to put as the Url a variable instead of a string.

Is that possible? For instance instead of make10.xml can I have a variable which equals to "make10.xml" or some other string?

$.ajax({
    type: "GET",
    url: "make10.xml",
    dataType: "xml",
    success: function(xml) {
    var select = $('#mySelect');


Yes

var url = "make10.xml";

$.ajax({
    type: "GET",
    url: url,
    dataType: "xml",
    success: function(xml) {
    var select = $('#mySelect');


Yes, certainly.

var u= 'make10.xml';
$.ajax({
    url: u,
    ...
});

Not really jQuery-related, this is basic JavaScript. The right-hand side of the colon in an object literal can be any value expression including variables.

It's only the left-hand side of an object literal that is unusual: when you say url it's a shorthand for the string literal 'url' instead of referring to a variable called url. If you want to be clearer about it you can write it out in full:

$.ajax({
    'url': u,
    ...
});

though it's arguable whether that's more or less readable.

0

精彩评论

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

关注公众号