我想学习使用ajax,但我的脚本没有一个工作:
<script>
function test()
{
var par = document.getElementById("nome").value;
alert ("i m here!"+par);
$.Ajax({
type: "POST",
url: "ProvaAJAX.php",
data: "par="+par,
success: function(msg){
alert( "Data Saved: " + msg );
}
});
}
</script>
选择项目:
<select id="nazione_pr" class="select-registrazione" onchange="prova()" name="nazione_pr">
输入项目:
<input id="nome" class="input-text" type="text"name="nome">
ProvaAJAX.php
<html>
<?php
echo "Test success". $_POST['par'];
?>
</html>
....我认为这个脚本应该没问题,但我的服务器不认为这个...... 当我更改“选择”值时,“我在这里”警告框出现但没有发生任何事情......
答案 0 :(得分:2)
很多问题
$ .ajax中的小写字母
数据:{“par”:par},
你使用jQuery所以使用var par = $(“#nome”)。val();
您的功能被称为“你称之为”的其他功能
在IE或Chrome中点击F12或在Firefox中安装firebug并在那里点击F12以查看控制台
<script>
$(function() { // when the page has loaded
$("#nazione_pr").on("change",function() {
var nazione = $(this).val(); // the select's value
var par =$("#nome").val(); // the textfield's value
$.ajax({
type: "POST",
url: "ProvaAJAX.php",
data: {"par":par,"nazione":nazione},
success: function(msg){
alert( "Data Saved: " + msg );
}
});
});
});
</script>
答案 1 :(得分:0)
请更改onchange =&#34; prova()&#34; to onchange =&#34; test()&#34;
<script>
function test()
{
var par = document.getElementById("nome").value;
$.ajax({
type: "POST",
url: "ProvaAJAX.php",
data: { par:par,fun:"test" }
}).done(function( data )
{
alert( "Data Saved: " + data );
});
}
</script>
和php文件: -
if($_REQUEST['fun'] == "test")
{
echo $_REQUEST['par'];
}
答案 2 :(得分:0)
// Launch AJAX request.
$.ajax(
{
// The link we are accessing.
url: jLink.attr( "href" ),
// The type of request.
type: "get",
// The type of data that is getting returned.
dataType: "html",
error: function(){
ShowStatus( "AJAX - error()" );
// Load the content in to the page.
jContent.html( "<p>Page Not Found!!</p>" );
},
beforeSend: function(){
ShowStatus( "AJAX - beforeSend()" );
},
complete: function(){
ShowStatus( "AJAX - complete()" );
},
success: function( strData ){
ShowStatus( "AJAX - success()" );
// Load the content in to the page.
jContent.html( strData );
}
}
);
// Prevent default click.
return( false );
}
);