2012-03-29 96 views
0

可能重複:
Get URL parameter with jQuery如何從Ajax響應檢索GET變量的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(); 
}); 
+0

檢查這篇文章:如何獲得與jQuery GET和POST變量?] [1] [1]:http://stackoverflow.com/questions/439463/how-to-get-get-and-post-variables-with-jquery – Nathan 2012-03-29 22:09:18

+0

嗯,我沒有最終找到這個問題的解決方案,所以我結束了而不是從我的php文件輸出json。 – greatwitenorth 2012-03-30 15:24:06

回答

0

AJAX在默認情況下將返回內容從請求。你想要的是標題

如果給出的返回地址爲http://example.com?error=1,則表示這是作爲重定向標題返回的。

爲了得到一個AJAX請求,已經在這裏找到答案的標題信息: jQuery and AJAX response header

+0

剛剛嘗試過,但它像其所響應的頭文件不包含GET變量:Date:Thu,29 Mar 2012 22:19:50 GMT Content-Encoding:gzip X-Powered-By:PHP/5.3 0.8 X-pingback的:http://example.com/xmlrpc.php 連接:關閉 雜注:無緩存 的Last-Modified:週四,2012年3月29日22時19分50秒GMT 服務器:Apache/2.2 .3(CentOS) Vary:Accept-Encoding,Cookie,User-Agent Content-Type:text/html; charset = UTF-8 Cache-Control:no-cache,must-revalidate,max-age = 0 傳輸編碼:分塊 有效期至:星期三,1984年1月11日05:00:00 GMT – greatwitenorth 2012-03-29 22:26:54