简体   繁体   English

Ajax请求重定向到index.html

[英]Ajax request redirects to index.html

I wrote the below code to make an ajax request to a link as shown in the below code .Instead of making an ajax call using POST request . 我编写了以下代码以对链接进行ajax请求,如下面的代码所示,而不是使用POST request进行ajax调用。 The page gets redirected to index.html with the link in the code in the address bar .. Why is it so? 通过地址栏中代码中的链接将页面重定向到index.html ..为什么会这样? pls help 请帮助

<script type="text/javascript" language="javascript">

$(document).ready(function() {

  $("#button").click(function() {
    $("#form1").validationEngine();
    if($('div input[type=text]').val() != "")
    {
      var textfield2=document.getElementById("textfield2").value;
      var textarea=document.getElementById("textarea").value;
      var urls="http://a_webite.com/pp.php?textfield2="+encodeURIComponent(textfield2)+"&textarea="+encodeURIComponent(textarea);
      //alert(url);
      $.ajax({
        type: "POST",
        url: urls,
        success: function(result) {
          $("#Sales_Body").empty().html("<h2>Your request has been received ");
        }
      });
    }
  }); 
});
</script>

event is undefined. event未定义。 Add it as a parameter for the click handler. 将其添加为单击处理程序的参数。

$("#button").click(function(event){
     event.preventDefault();

At the moment the click handler fails, and I assume the form gets submitted normally rather than via Ajax. 目前,点击处理程序失败了,我认为表单是正常提交​​的,而不是通过Ajax提交的。

一种简单的方法也是将按钮输入类型更改为button,而不是提交。

        <input type="button" value="Hit me /> 

Why are you setting type to 'POST' if all data is passed through url ? 如果所有数据都通过url传递,为什么将type设置为'POST'? You can use type 'GET', set url to ' http://a_webite.com/pp.php ' only and add a data parameter : 您可以使用“ GET”类型,仅将url设置为“ http://a_webite.com/pp.php ”并添加数据参数:

  $.ajax({
    type: "GET",
    url: "http://a_webite.com/pp.php",
    data: {'textfield2': $('#textfield2').val(), 'textarea': $('#textarea').val()},
    success: function(result) {
      $("#Sales_Body").empty().html("<h2>Your request has been received ");
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM