Ajax:如何在数据字段中使用变量

时间:2016-12-21 13:18:08

标签: jquery ajax

我有一个ajax函数,可以从数据库中获取一些图形数据。 它工作正常,但我想使它通用。

function getGraphData() {
    $.ajax({
        url: "/wordpress/wp-admin/admin-ajax.php",
        method: "POST",
        data: { action: "databaseName" },
        success: function (response) {
            //Some code;
        }
    })
} 

因此,为了使函数通用,我需要将数据库名称传递给函数而不是硬代码,但我似乎无法使其工作。

我将代码更改为:

函数调用:

 var dbname = "action: " + "mydatabase";
 getGraphData(dbname); 

更改功能:

function getGraphData(database) {
    $.ajax({
        url: "/wordpress/wp-admin/admin-ajax.php",
        method: "POST",
        data: database,
        success: function (response) {
            //Some code;
        }
    })
}  

我哪里出错?

此致 弗莱明。

4 个答案:

答案 0 :(得分:4)

执行"action: " + "mydatabase"不会创建对象,它会创建一个字符串。输出结果如下:

"action: mydatabase"

您想要的是以下内容:

{ "action" : "mydatabase" }

您需要执行以下操作:

var dbname = { action :"mydatabase" }

将您的功能组合成一个完整的示例,它将是这样的:

var database_name = "database1";
var data = { action : database_name }

getGraphData(data);  
// Your original function
function getGraphData(database) {
    $.ajax({
        url: "/wordpress/wp-admin/admin-ajax.php",
        method: "POST",
        data: database,
        success: function (response) {
            //Some code;
        }
    })
}  

答案 1 :(得分:1)

function getGraphData(database) {
    $.ajax({
        url: "/wordpress/wp-admin/admin-ajax.php",
        method: "POST",
        data: {action: database},  <-- change this line
        success: function (response) {
            //Some code;
        }
    })
}  

答案 2 :(得分:0)

尝试仅传递值

C:\Python27\Scripts>django-admin startproject python_tutorial
Traceback (most recent call last):
  File "c:\python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "c:\python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Scripts\django-admin.exe\__main__.py", line 9, in <module>
  File "c:\python27\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "c:\python27\lib\site-packages\django\core\management\__init__.py", line 316, in execute
    settings.INSTALLED_APPS
  File "c:\python27\lib\site-packages\django\conf\__init__.py", line 53, in __getattr__
    self._setup(name)
  File "c:\python27\lib\site-packages\django\conf\__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "c:\python27\lib\site-packages\django\conf\__init__.py", line 97, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "c:\python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
ImportError: Import by filename is not supported.

答案 3 :(得分:0)

您可以传递完整的对象:

db = { action: "myDatabase" };
getGraphData(db);

function getGraphData(dbObject) {
    $.ajax({
        url: "/wordpress/wp-admin/admin-ajax.php",
        method: "POST",
        data: dbObject,
        success: function (response) {
            //Some code;
        }
    })
}

OR(如果对象始终相同)只需传递实际的数据库名称:

name = "myDatabase";
getGraphData(name);

function getGraphData(dbName) {
    $.ajax({
        url: "/wordpress/wp-admin/admin-ajax.php",
        method: "POST",
        data: { action: dbName },
        success: function (response) {
            //Some code;
        }
    })
}