如何从jquery中的ajax响应中检索GET变量

时间:2012-03-29 22:03:30

标签: javascript jquery ajax post get

  

可能重复:
  Get URL parameter with jQuery

我有一个通过ajax发布数据的表单。响应以URL中的GET变量的形式返回。例如,如果写入表单数据失败,则返回地址为:http://example.com/?error=1

我如何使用jquery检查这个。现在,当我在msg变量上执行console.log时,我只得到example.com的html输出(我觉得这很有意义)。我需要GET变量。我怎么做到这一点?

$('#wp_email_capture').submit(function(e){
    var email = $('#wp-email-capture-email').val();
    $.ajax({
      type: "GET",
      url: "/",
      data: "wp_capture_action=1&wp-email-capture-email=" + email
    }).done(function( msg ) {
        console.log(msg)
    });
    e.preventDefault();
});

1 个答案:

答案 0 :(得分:0)

默认情况下,AJAX将从请求中返回内容。你想要的是标题

如果您的返回地址为http://example.com?error=1,那么这意味着它将作为重定向标头返回。

要从AJAX请求中获取标头信息,请在此处回答: jQuery and AJAX response header