Javascript $.ajax 如果条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14762775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:49:28  来源:igfitidea点击:

$.ajax if condition

javascriptjquery

提问by olo

I failed to write a condition inside of ajax by using the following syntax.

我无法使用以下语法在 ajax 中编写条件。

      var num = 1;
      $.ajax({
          type: "POST",
      //condition starts
        if (num === 1){
          url: url1,
          data: data1,
        }else{
          url: url2,
          data: data2,
        }
        //condition finishes
          success: success,
          dataType: dataType
        });

but this way works.

但这种方式有效。

 var num = 1;
if(num === 1){
    $.ajax({
  type: "POST",
  url: url1,
  data: data1,
  success: success,
  dataType: dataType
});
}else{
    $.ajax({
  type: "POST",
  url: url2,
  data: data2,
  success: success,
  dataType: dataType
});
}

the 2nd method is not quite ideal as repeating my code. is my first script in a wrong syntax? Could someone please point out? thanks

第二种方法不是很理想,因为重复我的代码。我的第一个脚本语法错误吗?有人可以指出吗?谢谢

回答by Bergi

is my first script in a wrong syntax?

我的第一个脚本语法错误吗?

Yes, absolutely. You were just inserting if-else-statement parts in the middle of an object literal. You should use something like this:

是的,一点没错。您只是在对象文字中间插入 if-else-statement 部分。你应该使用这样的东西:

var params = {
    type: "POST",
    success: success,
    dataType: dataType
};
if (num == 1) {
    params.url = url1;
    params.data = data1;
} else {
    params.url = url2;
    params.data = data2;
}
$.ajax(params);

Or if you want to inline them, you can use the ternary operator:

或者如果你想内联它们,你可以使用三元运算符

$.ajax({
    type: "POST",
    url: (num == 1) ? url1 : url2,
    data: (num == 1) ? data1 : data2,
    success: success,
    dataType: dataType
});

(If you don't want to repeat the condition, store its boolean result in a variable)

(如果您不想重复条件,请将其布尔结果存储在变量中)

回答by Evan Trimboli

You could do it like this:

你可以这样做:

var num = 1, url, data;

if (num === 1) {
    url = url1;
    data = data1;
} else {
    url = url2;
    data = data2;
}

$.ajax({
    type: "POST",
    url: url,
    data: data,
    success: success,
    dataType: dataType
});

回答by Plynx

The stuff in the {braces }is an object literal. You can declare it and modify it before you call $.ajax.

在东西{括号}是一个对象字面。您可以在调用之前声明并修改它$.ajax

var options =
{
    type: "POST",
    url: url2,
    data: data2,
    success: success,
    dataType: dataType
};

if (num === 1) { options.url = url; options.data = data; }

$.ajax(options);

回答by nneonneo

$.ajaxtakes a regular JavaScript object, so you can fill it in piecewise:

$.ajax接受一个普通的 JavaScript 对象,所以你可以分段填充它:

request = {type: "POST", success: success, dataType: dataType};
if(num == 1) {
    request.url = url1;
    request.data = data1;
} else {
    request.url = url2;
    request.data = data2;
}
$.ajax(request);

回答by allenhwkim

Try this way if url and data are very simple.

如果 url 和 data 非常简单,请尝试这种方式。

  var num = 1;
  $.ajax({
      type: "POST",
      url : (num==1? url1 : url2),
      data: (num==1? data1 : data2),
      success: success,
      dataType: dataType
    });

回答by Yogu

Place the condition before the ajax statements and assign common variables there.

将条件放在 ajax 语句之前并在那里分配公共变量。

回答by Sahil Grover

Try this:

尝试这个:

var num = 1;
$.ajax({
    type: "POST",
    url: (num === 1 ? url1 : url2)
    data: (num === 1 ? data1 : data2)
    success: success,
    dataType: dataType
});

But as others have mentioned it would be best to just assign variables outside of the ajax call.

但正如其他人所提到的,最好只在 ajax 调用之外分配变量。