Javascript 使用AJAX的JQuery可以';无法从php页面获取内容
使用AJAX的JQuery无法从php页面获取内容 我试着按下提交按钮,没有任何变化。不知道为什么 这是我的两页 Jquery.phpJavascript 使用AJAX的JQuery可以';无法从php页面获取内容,javascript,php,Javascript,Php,使用AJAX的JQuery无法从php页面获取内容 我试着按下提交按钮,没有任何变化。不知道为什么 这是我的两页 Jquery.php <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="http://code.jquer
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
$("#input").click(function(){
$.ajax({
url: 'jquery2.php?keyword='+$("#fname").val(),
type: 'GET',
success: function (data) {$("#newhtml").val(data);}
});
})
});
</script>
</head>
<body>
First name: <input type="text" id="fname" name="fname"><br>
<input type="button" id="input" value="Search Data">
<div id="newhtml"></div>
</body>
</html>
<?php
$advert = array(
'ajax' => 'Hello world!',
'advert' => $row['adverts'],
);
echo json_encode($advert);
?>
$(文档).ready(函数(){
$(“#输入”)。单击(函数(){
$.ajax({
url:'jquery2.php?关键字='+$(“#fname”).val(),
键入:“GET”,
成功:函数(数据){$(“#newhtml”).val(数据)}
});
})
});
名字:
Jquery2.php
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
$("#input").click(function(){
$.ajax({
url: 'jquery2.php?keyword='+$("#fname").val(),
type: 'GET',
success: function (data) {$("#newhtml").val(data);}
});
})
});
</script>
</head>
<body>
First name: <input type="text" id="fname" name="fname"><br>
<input type="button" id="input" value="Search Data">
<div id="newhtml"></div>
</body>
</html>
<?php
$advert = array(
'ajax' => 'Hello world!',
'advert' => $row['adverts'],
);
echo json_encode($advert);
?>
我的代码有什么问题吗?这
$("#newhtml").val(data);
应该是
$("#newhtml").html(data);
// OR
$("#newhtml").append(data);
您可能还需要添加
dataType: "json",
contentType: "application/json"
在成功回调之前,将属性添加到ajax方法
更新
总的来说,头标签中的代码应该是这样的
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#input").click(function(){
$.ajax({
url: 'jquery2.php?keyword='+$("#fname").val(),
type: 'GET',
dataType: "json",
contentType: "application/json",
success: function (data) {
$("#newhtml").html(data);
}
});
});
});
</script>
</head>
$(文档).ready(函数(){
$(“#输入”)。单击(函数(){
$.ajax({
url:'jquery2.php?关键字='+$(“#fname”).val(),
键入:“GET”,
数据类型:“json”,
contentType:“应用程序/json”,
成功:功能(数据){
$(“#newhtml”).html(数据);
}
});
});
});
尝试更改
success: function (data) {$("#newhtml").val(data);}
到
不要使用多个jquery源。 请仅使用您引用的两个选项中的一个
对于非输入标记,必须使用.html()或.text()Jquery2.php抛出错误: “注意:第4行[…]/Jquery2.php中的未定义变量:行” $row['adverts']未定义 此外,更改如下: $("#newhtml").val(data); $(“#newhtml”).val(数据); 到 $(“#newhtml”).html(数据);