在ajax调用的参数中传递变量

时间:2022-11-15 00:23:03

How can I pass the variables in the parameters of ajax call. Below is the example to clarify my question:-

如何在ajax调用的参数中传递变量。以下是澄清我的问题的例子: -

function updateSingleParameters(name,varName, varValue)
{
$.ajax({
    type: 'POST',
    url:name,
    data: {
        varName: varValue
    },
    success: function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    }

});

I need varName also to be treated as a variable but it is treated as a constant when I execute the script.

我还需要将varName视为变量,但在执行脚本时它被视为常量。

Kindly suggest.

Thanks in advance.

提前致谢。

4 个解决方案

#1


2  

Create an object and set the property name with the varName variable using [] syntax.

使用[]语法创建一个对象并使用varName变量设置属性名称。

function updateSingleParameters(name,varName, varValue)
{
    var data = {};
    data[varName] = varValue;
    $.ajax({
        type: 'POST',
        url:name,
        data: data,
        success: function(data, status){
            alert("Data: " + data + "\nStatus: " + status);
        }

    });
}

#2


0  

The basic idea is create an object and set the key with bracket notation.

基本思想是创建一个对象并使用括号表示法设置键。

var data = {};
data[varName] = varValue;
$.ajax({
    type: 'POST',
    url:name,
    data: data,
    success: function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    }

});

#3


0  

In your function, I think if you do:

在你的功能中,我想如果你这样做:

   function updateSingleParameters(name,varName, varValue)
   {
     var passToAjax = varValue;
      $.ajax({
         type: 'POST',
         url:name,
         data: {
         varName: passToAjax
       },
       success: function(data, status){
       alert("Data: " + data + "\nStatus: " + status);
       }

     });

I think it would work better. But i'd need to see your HTML also to make sure that it is the problem. Also, the file where you send the ajax.

我认为它会更好。但我还需要看到你的HTML,以确保它是问题所在。此外,您发送ajax的文件。

#4


0  

Construct the object separately.

单独构造对象。

function updateSingleParameters(name,varName, varValue)
{
var obj = {};
obj[varName] = varValue;

$.ajax({
    type: 'POST',
    url:name,
    data: obj,
    success: function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    }

});

#1


2  

Create an object and set the property name with the varName variable using [] syntax.

使用[]语法创建一个对象并使用varName变量设置属性名称。

function updateSingleParameters(name,varName, varValue)
{
    var data = {};
    data[varName] = varValue;
    $.ajax({
        type: 'POST',
        url:name,
        data: data,
        success: function(data, status){
            alert("Data: " + data + "\nStatus: " + status);
        }

    });
}

#2


0  

The basic idea is create an object and set the key with bracket notation.

基本思想是创建一个对象并使用括号表示法设置键。

var data = {};
data[varName] = varValue;
$.ajax({
    type: 'POST',
    url:name,
    data: data,
    success: function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    }

});

#3


0  

In your function, I think if you do:

在你的功能中,我想如果你这样做:

   function updateSingleParameters(name,varName, varValue)
   {
     var passToAjax = varValue;
      $.ajax({
         type: 'POST',
         url:name,
         data: {
         varName: passToAjax
       },
       success: function(data, status){
       alert("Data: " + data + "\nStatus: " + status);
       }

     });

I think it would work better. But i'd need to see your HTML also to make sure that it is the problem. Also, the file where you send the ajax.

我认为它会更好。但我还需要看到你的HTML,以确保它是问题所在。此外,您发送ajax的文件。

#4


0  

Construct the object separately.

单独构造对象。

function updateSingleParameters(name,varName, varValue)
{
var obj = {};
obj[varName] = varValue;

$.ajax({
    type: 'POST',
    url:name,
    data: obj,
    success: function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    }

});