jQuery $ .ajax不显示任何内容 [英] jQuery $.ajax doesn't show anything
问题描述
我将使用AJAX和PHP文件处理HTML表单来管理数据,但是成功消息或错误消息均无效. 我试过萤火虫,但好像它在$ .ajax语句出现时等待一些东西,然后崩溃".
I'm going to work with a HTML form using AJAX and a PHP file to manage data but neither success message or error message work. I tried firebug but it's like it waits something when occur the $.ajax statement then it "crashes".
<form id="alcoholic-form" name="alcoholic-form" method="post">
<fieldset>
<input type="submit" id="submit" value="Invia">
</fieldset>
</form><!-- form -->
<div id="risultato"></div>
<div id="errore"></div>
controller.js文件:
The controller.js file:
$(document).ready(function(){
$("#submit").click(function(){
//var formData = $("#alcoholic-form").serialize();
var nome = "nico";
var cognome = "basi";
$.ajax({
type: "POST",
url: "prova.php", //data manager
data: "nome=" + nome + "&cognome=" + cognome, //data to server
dataType: "html", //return value format
success:function(msg){
$("#risultato").html(msg);
alert(msg);
},//success
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});//ajax
});//submit click
});//document ready
然后是prova.php文件:
And then the prova.php file:
<?php /* questionario.php */ ?>
<?php
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
echo "nome = " . $nome . " cognome = " . $cognome;
?>
提前谢谢
推荐答案
您有一个带有提交"按钮的表单,当您单击提交"按钮时,默认操作是提交表单.
You have a form with a submit button, when you click on the submit button the default action is to submit the form.
由于您正在使用ajax请求,因此需要阻止默认表单提交.您可以调用event.preventDefault()来做到这一点.
Since you are using ajax request you need to prevent the default form submit. You can call event.preventDefault() to do that.
$(document).ready(function () {
$("#submit").click(function (e) {
//prevent the default form submit
e.preventDefault();
//var formData = $("#alcoholic-form").serialize();
var nome = "nico";
var cognome = "basi";
$.ajax({
type: "POST",
url: "prova.php", //data manager
data: "nome=" + nome + "&cognome=" + cognome, //data to server
dataType: "html", //return value format
success: function (msg) {
$("#risultato").html(msg);
alert(msg);
}, //success
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
}); //ajax
}); //submit click
}); //document ready
但是由于您有一个表单和一个提交按钮,因此最好处理表单的提交事件,而不是按钮的点击事件
But since you have a form and a submit button, it will be better to handle the submit event of the form that the click event of the button
$(document).ready(function () {
$("#alcoholic-form").submit(function (e) {
//prevent the default form submit
e.preventDefault();
//var formData = $("#alcoholic-form").serialize();
var nome = "nico";
var cognome = "basi";
$.ajax({
type: "POST",
url: "prova.php", //data manager
data: "nome=" + nome + "&cognome=" + cognome, //data to server
dataType: "html", //return value format
success: function (msg) {
$("#risultato").html(msg);
alert(msg);
}, //success
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
}); //ajax
}); //submit click
}); //document ready
这篇关于jQuery $ .ajax不显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!