我在 status.php 中有这个简单的代码:
<script type="text/javascript">
$(document).ready(function() {
$("form#iscrizione").submit(function(){
var ordine = $("#ordine").val();
var cognome = $("#cognome").val();
$.ajax({
type: "POST",
url: "http://****/new/action.php",
data: "cognome=" + cognome + "&ordine=" + ordine,
dataType: "html",
success: function(risposta) {
$("div#risposta").html(risposta);
alert("ok!");
},
error: function(){
alert("Chiamata fallita!!!");
}
});
return false;
});
});
</script>
在我的页面 (status.php) 中它有效!
但是当我在 index.php 中放置一个 status.php 框架时它不起作用并且只更改 url 如下:?cognome=Esposito&ordine=2121237391
我应该怎么做才能在 index.php 中作为框架工作?
最佳答案
我用过
type: "GET",
url: "http://stackoverflow.com/questions/32554239/ajax-post-without-refresh",
代替你的
type: "POST",
url: "http://****/new/action.php",
而且效果很好。 (直接点击“Run snippet”,会报错,因为url不支持跨域)
$(document).ready(function() {
$("form#iscrizione").submit(function(){
var ordine = $("#ordine").val();
var cognome = $("#cognome").val();
$.ajax({
type: "GET",
url: "http://stackoverflow.com/questions/32554239/ajax-post-without-refresh",
data: "cognome=" + cognome + "&ordine=" + ordine,
dataType: "html",
success: function(risposta) {
$("div#risposta").html(risposta);
alert("ok!");
},
error: function(){
alert("Chiamata fallita!!!");
}
});
return false;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="iscrizione">
<input type="text" value="get" id="cognome"><input type="text" value="get" id="ordine"><input type="submit" value="get"></form><div id="risposta"></div>
关于php - 无需刷新的 Ajax 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554239/