无法让JQuery AJAX的成功活动显示一个感谢页面

时间:2014-07-22 19:41:17

标签: javascript jquery ajax

我有这个代码,如果一切正常,结果应该是一个感谢页面。 这是代码:

<a class="large red button round" onclick="$.ajax({ type: 'POST', data:$('#newsletter').serialize(), url: 'http://www.stress-free-life.co.il/lists/?p=subscribe&    amp;id=1', success: function (msg) { openWin(); $('#Name').val(''); $('#email').val(''); }, failure: function (msg) { alert('Sorry, we were unable to process your subscription.'); } }); return false;" href="javascript; return false;">לחץ כאן</a>

我尝试过多个选项,但无法显示感谢页面。

2 个答案:

答案 0 :(得分:1)

嗯...让我们清理一下,onclick处理程序真的难以阅读,通常被认为是不好的做法

您的HTML:

<a class="large red button round my-button" href='#'>לחץ כאן</a>

在一个单独的JS文件中你应该:

$(function(){
  $('.my-button').on('click', function(e){
    e.preventDefault();

    $.ajax({
      type: 'POST',
      data: $('#newsletter').serialize(),
      url: 'http://www.stress-free-life.co.il/lists/?p=subscribe&id=1',
      success: function(msg){
        window.location = "/my-thank-you-page.html";
        //openWin();  // No idea what this function is...
        //$('#Name').val('');
        //$('#email').val('');
      },
      failure: function(msg){
        alert('Sorry, we were unable to process your subscription.');
      }
    });
  });
});

答案 1 :(得分:0)

也许是这样的......

<a id="ajaxpost" class="large red button round" href="javascript:void(0)">לחץ כאן<a>


<script>

$("#ajaxpost").click(function () {
    $.ajax({
        type: 'POST',
        data: $('#newsletter').serialize(),
        url: 'http://www.stress-free-life.co.il/lists/?p=subscribe&amp;id=1',
        success: function (msg) {
            openWin();

            $('#Name').val('');
            $('#email').val('');
            window.location = "url of thank-you page";
        },
        failure: function (msg) {
            alert('Sorry, we were unable to process your subscription.');
        }
    });
});

</script>