jQuery 用于发布和获取的 Ajax 教程

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

Ajax tutorial for post and get

jqueryajax

提问by Reham Fahmy

I need a simple ajax tutorial or case study for a simple input form, where I want to post a username through an input form, which sends it to the database and replies with the results.
Any recommendation for such tutorial is welcome, because I've only got one using Mootool but I'm searching for one using jQuery!

我需要一个简单的输入表单的简单 ajax 教程或案例研究,我想通过输入表单发布用户名,将其发送到数据库并回复结果。
欢迎任何对此类教程的推荐,因为我只有一个使用 Mootool,但我正在使用 jQuery 搜索一个!

回答by apis17

You can try this:

你可以试试这个:

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});

This code will append the content of test.htmlfile to #resultselement

此代码将test.html文件的内容附加到#results元素

You can find more information at jQuery website.

您可以在jQuery 网站上找到更多信息。

Update:

更新:

Use this code to send POST data and output result.

使用此代码发送 POST 数据并输出结果。

var menuId = $("ul.nav").first().attr("id");
var request = $.ajax({
  url: "script.php",
  type: "POST",
  data: {id : menuId},
  dataType: "html"
});

request.done(function(msg) {
  $("#log").html( msg );
});

request.fail(function(jqXHR, textStatus) {
  alert( "Request failed: " + textStatus );
});

回答by shaun5

Assuming you have some html like:

假设您有一些 html,例如:

<input type="text" name="username" id="username">
<div id="resultarea"></div>

You would use a <script>like:

你会使用一个<script>喜欢:

var myusername = $("#username").val();
$.ajax({
  type: "GET",
  url: "serverscript.xxx",
  data: myusername,
  cache: false,
  success: function(data){
     $("#resultarea").text(data);
  }
});