php - 无需刷新的 Ajax 发布

标签 php jquery ajax

我在 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/

相关文章:

javascript - 使用 JQuery Ajax 提交表单不在服务器中工作,但在本地主机中工作 codeigniter

php - FB App 必须使用页面访问 token 调用此方法

javascript - FormData 控制台记录 [object FormData ] 而不是实际文件

php - 如何在三列中显示 wordpress 帖子?

javascript - 如何在 oracle jet 中创建链接(动态?)选择字段?

javascript - 使用左连接在 while 循环中获取数据

php - Swift 从网站读取 HTML 标签

javascript - 为什么我的 .text() 操作在 AJAX 调用后不起作用?

javascript - JQuery 和 Console.log() 的奇怪行为

php - XMLHttpRequest 无法加载资源