我有一个真正的问题。我有一个php代码和一个表单,当表单被提交时,一个POST请求被发送,页面重新加载,当然post是在page.But中访问的,我想使用AJAX,以便页面不被刷新。我知道AJAX的基础知识,但我不想从beggining.Is构建所有的项目,有没有一种成功的方法,AJAX的函数链接到我的php代码??
$.ajax({
type: "POST",
url: "index.php",
datatype: "html",
data: dataString,
success: function(data) {
//How can i link here to start running my php code which is located in the same page.
}
});发布于 2013-10-01 01:46:27
$.ajax({
type: "POST",
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
// try this
console.log(data);
// see what 'data' actually is
}
});然后点击F12在浏览器中查看控制台。
你确定你想要html的数据类型吗?你可能想要一个json或XML的数据类型,在ajax post之后服务器会返回给你什么呢?
发布于 2013-10-01 01:46:48
您必须取消表单的提交,这样ajax请求才会发生,否则将被取消。还可以使用.serialize获取表单数据的名称-值对字符串,以便在ajax调用中使用。
Html
<form id="MyForm">
<button id="MyButtonId">Submit</button>
</form>JS
$("#MyForm").submit(function(e){
//Prevents the form from being submitted
e.preventDefault();
$.ajax({
type: "POST",
data: $("#MyForm").serialize(),
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
alert(data);
}
});
});https://stackoverflow.com/questions/19100467
复制相似问题