简体   繁体   English

Jquery ajax函数如何传递变量

[英]Jquery ajax function how to pass variable

Hi i'm new in jquery i have wriiten code and i want to pass variable to test.html page how can i do this can any one help嗨,我是 jquery 的新手,我有编写代码,我想将变量传递给 test.html 页面,我该怎么做,任何人都可以提供帮助

my code我的代码

$.ajax({
      url: "test.html",
      context: document.body
    }).done(function() {
      $(this).myClass( "done" );
    });

AJAX (Asynchronous JavaScript and Xml) is for communicating with a server. AJAX (异步 JavaScript 和 Xml)用于与服务器通信。 The following is an AJAX POST request that is being sent to test.php.以下是发送到 test.php 的 AJAX POST 请求。 PHP runs on servers and can receive, process, and respond to HTTP requests. PHP 在服务器上运行,可以接收、处理和响应 HTTP 请求。 You may want to look into PHP and server side web communications.您可能想要研究 PHP 和服务器端 Web 通信。

var myVar = "test";

 $.ajax({
  url: "test.php",
  type: "POST",
  data:{"myData":myVar}
}).done(function(data) {
     console.log(data);
});

The accompanying PHP file may look something like:附带的PHP文件可能看起来这样

<?php
    $data = isset($_REQUEST['myData'])?$_REQUEST['myData']:"";
    echo $data;
?>

These are very basic examples but can be very useful to learn.这些是非常基本的示例,但对学习非常有用。

AJAX tutorial: http://www.w3schools.com/ajax/ PHP tutorial: http://www.codecademy.com/en/tracks/php AJAX 教程: http : //www.w3schools.com/ajax/ PHP 教程: http : //www.codecademy.com/en/tracks/php

     var quantity = $(this).data("quantity");
    // you can get data use of j query

     $.ajax({
      url: "xyx.php?action=add&",
      type: "POST",
      data:{"product_id":product_id,"qty":quantity}
    });

** data:{"product_id":product_id,"qty":quantity} here list of argument accept php code depends on your backed logics so.** ** data:{"product_id":product_id,"qty":quantity} 这里的参数列表接受 php 代码取决于您支持的逻辑。**

Create a variable with key:value pairs in JSON format and pass it to the "data" parameter of your ajax call.使用 JSON 格式的键值对创建一个变量,并将其传递给 ajax 调用的“数据”参数。 It will be passed in the post variables, for a POST, and will be in the request line, for a GET.它将在 post 变量中传递,对于 POST,并将在请求行中,对于 GET。

var options = { "name1" : "value1", "name2" : "value2"};

$.ajax({
  url: "test.html",
  context: document.body,
  data: options
}).done(function() {
  $(this).myClass( "done" );
});

so you scope a variable outside your ajax request and refer to it when making that request.因此,您将变量范围限定在 ajax 请求之外,并在发出该请求时引用它。 You then can concatenate the string inside the ajax request like i think you are trying too.然后,您可以连接 ajax 请求中的字符串,就像我认为您也在尝试一样。

var endingUrl = "/help.html";

$.ajax({
  url: "test"+endingUrl,
  context: document.body,
  data: options
}).done(function() {
  console.log(endingUrl);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM