简体   繁体   中英

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

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. The following is an AJAX POST request that is being sent to test.php. PHP runs on servers and can receive, process, and respond to HTTP requests. You may want to look into PHP and server side web communications.

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
    $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

     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.**

Create a variable with key:value pairs in JSON format and pass it to the "data" parameter of your ajax call. It will be passed in the post variables, for a POST, and will be in the request line, for a 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. You then can concatenate the string inside the ajax request like i think you are trying too.

var endingUrl = "/help.html";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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