jQuery 通过 AJAX 发送数据

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

Sending data via AJAX

jqueryajaxsend

提问by user3341435

i have a build a javascript which does following: Get content via ajax->php->sql and show it on index.php after clicking the content there will be shown new content.

我构建了一个 javascript,它执行以下操作:通过 ajax->php->sql 获取内容并在单击内容后将其显示在 index.php 上,将显示新内容。

Now I want to have a function which sends data after content is clicked to a php which will do something in in the db. How can i create a function which will send data? Thank you!

现在我想要一个函数,它在内容被点击后发送数据到一个 php,它将在 db 中做一些事情。如何创建一个发送数据的函数?谢谢!

This is my code which shows content:

这是我显示内容的代码:

<script id="source" language="javascript" type="text/javascript">
function laden(){
$(function() 
{

$.ajax({                  
`usr_id`                    
  url: 'content/get.php',              


  dataType: 'json',                   
  success: function(data)         
  {
    var id = data[0];             
    var name = data[1];

    var count = data[3];


    $('#output').html('<div onclick="laden('+id+')" id="content"></div>');

  } 
});

}); } `

}); }`

回答by ethan

You can send data to the callback script, specified in the URL, by including values in the jQuery.ajax datasetting. Depending on what type of request you're making, this data will either be included in the $_GETor $_POSTglobal variables. For example, to POST data to your callback script, you could do:

通过在 jQuery.ajaxdata设置中包含值,您可以将数据发送到 URL 中指定的回调脚本。根据您发出的请求类型,此数据将包含在$_GET$_POST全局变量中。例如,要将数据 POST 到您的回调脚本,您可以执行以下操作:

$.ajax({                    
  url: 'content/get.php',     
  type: 'post', // performing a POST request
  data : {
    data1 : 'value' // will be accessible in $_POST['data1']
  },
  dataType: 'json',                   
  success: function(data)         
  {
    // etc...
  } 
});

For more information, please read up on the jQuery.ajax function's documentation at https://api.jquery.com/jQuery.ajax/

有关更多信息,请阅读https://api.jquery.com/jQuery.ajax/上的 jQuery.ajax 函数文档