Jquery 外部$.ajax函数,具有url、方法数据和返回到pass的数据等参数

Jquery 外部$.ajax函数,具有url、方法数据和返回到pass的数据等参数,jquery,html,ajax,Jquery,Html,Ajax,我正在尝试创建一个大量使用ajax(jquery)的网站。为此,我想使用一个外部$.ajax函数,并在需要时传递参数。我想要一个完整的例子来指导我。我在服务器端使用php,返回的数据将是json。我在服务器端没有问题,但无法让任何ajax工作。请在例子方面给予任何帮助,我们将不胜感激 我做的第一个例子如下 <!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <meta name

我正在尝试创建一个大量使用ajax(jquery)的网站。为此,我想使用一个外部$.ajax函数,并在需要时传递参数。我想要一个完整的例子来指导我。我在服务器端使用php,返回的数据将是json。我在服务器端没有问题,但无法让任何ajax工作。请在例子方面给予任何帮助,我们将不胜感激

我做的第一个例子如下

<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>


$(document).ready(function(){

        $("#find").click(function(){

            $.ajax({
                // the URL for the request
                url: "findPatient.php",
                // the data to send (will be converted to a query string)
                data: {pnhsno: $('#search').val()},
                // whether this is a POST or GET request
                type: "GET",
                // the type of data we expect back
                dataType : "json",


                // code to run if the request succeeds;
                // the response is passed to the function
                success: function(json){

                    $("#ex1").val(json[0].firstname);

                }


            });


        });
});

</script>


<link rel = "stylesheet" type ="text/css" href = "pascss/patient.css " >
</head>
<body>

<div id="wrapper">  

<form class="form-patient" ">
<fieldset>
<legend>Patient Details</legend>
<label for="ex1">First name</label>
<input type="text" name="ex1" id="ex1" />
</fieldset>
</form>

<form class="form-wrapper">
<fieldset>
<legend>Find Patient</legend>
    <input type="text" id="search" placeholder="Enter patient NHS number" required>
    <input type="submit" value="Find" id="find" onclick =">


    </fieldset>
</form>



</div>

</body>
</html>

$(文档).ready(函数(){
$(“#查找”)。单击(函数(){
$.ajax({
//请求的URL
url:“findPatient.php”,
//要发送的数据(将转换为查询字符串)
数据:{pnhsno:$('#search').val()},
//这是一个POST还是GET请求
键入:“获取”,
//我们期望返回的数据类型
数据类型:“json”,
//请求成功时要运行的代码;
//响应被传递给函数
成功:函数(json){
$(“#ex1”).val(json[0].firstname);
}
});
});
});
即使这个例子也不完全有效。似乎它正在找回正确的json
并在完成文档之前在文本框中显示一毫秒。就绪功能。完成后,将再次擦除附加在文本框上的文本。我就是不明白。有人能给我解释一下吗。非常感谢

提交表单将加载一个新页面并删除现有页面

如果JavaScript成功,则阻止默认表单操作

// Capture the event object
...click(function(event){
然后,最后:

event.preventDefault();