jQuery ajax调用JQUERY中的变量数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11687217/
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-26 10:50:23  来源:igfitidea点击:

Variable data in ajax call JQUERY

jqueryajax

提问by fawad

I'm trying to use variable in AJAX call in jquery but it is not working. move variable contains different values. Please check the following code:

我正在尝试在 jquery 的 AJAX 调用中使用变量,但它不起作用。移动变量包含不同的值。请检查以下代码:

var $move = 'next';

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: {$move:1},
});

Suggest any way to use $move variable in data.

建议在数据中使用 $move 变量的任何方法。

采纳答案by AdityaParab

It should be

它应该是

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: {"data":$move},
});

回答by Wolfram

If you want to use the variable as the name of the property, use array notation.

如果要将变量用作属性的名称,请使用数组表示法。

There are two ways to access object members: dot notation and bracket notation (a.k.a. subscript operator).

有两种方法可以访问对象成员:点表示法和括号表示法(又名下标运算符)。

Your code with array notation:

带有数组符号的代码:

var $move = 'next';
var data = {};
data[$move] = 1;

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: data,
});

The example on jsfiddle(the post obviously doesn't work, so check the console to see what gets posted.)

jsfiddle 上的示例(该帖子显然不起作用,因此请检查控制台以查看发布的内容。)

回答by Sem

If you want to have a variable-variablein your POST request, you will need to make a seperateJSON object:

如果你想在你的 POST 请求中有一个变量,你需要创建一个单独的JSON 对象:

var name = 'next';
var dataObject = {};
dataObject[name] = 1;

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: dataObject,
   complete : function() {
     // success!
   }
});

回答by Subir Kumar Sao

You can do something like data: $move+"=1"

你可以做类似的事情 data: $move+"=1"

回答by John Grabauskas

I was looking for something like this. I wanted to have a variable for the key AND a variable for the value.

我正在寻找这样的东西。我想有一个键的变量和一个值的变量。

let dataPair = {};
dataPair[dataKey] = dataValue;
$.ajax({
  url: TheAPIPath,
  data: dataPair,
  method: "GET",
(etc)
let dataPair = {};
dataPair[dataKey] = dataValue;
$.ajax({
  url: TheAPIPath,
  data: dataPair,
  method: "GET",
(etc)

回答by dexter.ba

To post the value of $move variable, do this:

要发布 $move 变量的值,请执行以下操作:

$.ajax({
   type: "POST",
   url: "somephp.php",
   data: {move: $move}
});

回答by Kris.Mitchell

It looks like you are trying to use a PHP variable in javascript. You can use .serialize to gather data and pass it to the ajax call. But as for making calls with different named variables for the name of the value pair, you will need to gather that information from the php page you are passing.

看起来您正在尝试在 javascript 中使用 PHP 变量。您可以使用 .serialize 收集数据并将其传递给 ajax 调用。但至于使用不同的命名变量调用值对的名称,您需要从传递的 php 页面收集该信息。

Example:

例子:

$.get("jQueryFunctions.php?action=checkCaptula",$('#resetPasswordDiv :input').serialize()).done(function(data){ ....

While this is a .get instead of a .ajax, it's just shorthand for the .ajax call. The passwordDiv contains HTML inputs with names and id. It gathers the information and passes it to the php page for processing.

虽然这是 .get 而不是 .ajax,但它只是 .ajax 调用的简写。passwordDiv 包含带有名称和 ID 的 HTML 输入。它收集信息并将其传递给 php 页面进行处理。