2010-09-14 59 views
0

我使用Wordpress 3和jQuery 1.4.2。 Firebug告訴我$ .parseJSON不是一個函數,我很難理解爲什麼。jQuery - parseJSON不是一個函數嗎?

任何建議表示讚賞。

$(document).ready(function(){ 
$('#subscribe_form_submit').click(function(){ 

    function updatePage(theResponse, textStatus, XMLHttpRequest){ 
     theResponse = $.parseJSON(theResponse); 
     console.log(theResponse); 

     if(theResponse == "OK!"){ 
      $('#subscribe').fadeOut('fast', function(){ 
       $('#subscribe').html("<br /><h3 style=\"text-align:center;border:1px solid #fff;background-color:#d3eefe;padding:8px;\">Thanks!</h3><br /><br />"); 
       $('#subscribe').fadeIn('slow'); 
      }); 
     } 
     else{ 
      theResponse = $.parseJSON(theResponse); 
      console.log(theResponse); 

     } 
    } 

    var theData = $('#subscribe').serialize(); 
    $.ajax({ 
     type: 'GET', 
     url: 'http://www.foo.com/wp-content/themes/thesis_17/custom/subscribe.php?' + theData, 
     dataType: 'json', 
     success: updatePage(), 
     error: function(xhr, textStatus, errorThrown){ 
      console.log((errorThrown ? errorThrown : xhr.status)); 
     } 
    }) 

}); 

});

+0

查看API文檔http://api.jquery.com/jQuery.parseJSON/, – Felix 2010-09-14 14:46:27

+1

第二行最後一行缺少分號。補充說,也許這可以解決問題... – davehauser 2010-09-14 14:46:29

回答

2

我不知道你爲什麼得到這個錯誤信息,但我不認爲這是真正的問題。

您正在調用updatePage函數,而不是將其放入對象中。名稱後刪除括號:

success: updatePage, 

當你調用的函數,你做AJAX調用之前,並且不帶任何參數,您嘗試解析theResponse參數是不確定的。

此外,由於您在AJAX調用中使用的數據類型爲json,因此根本不必解析數據,這在調用成功回調函數時已經完成。

1

您在$.ajax調用中添加了dataType:'json'。這意味着jQuery會爲你解析JSON。另外你爲什麼要撥打$.parseJSON兩次?

此外,像迪克說,你可以使用$.getJSON速記。

$.getJSON('http://www.foo.com/wp-content/themes/thesis_17/custom/subscribe.php?' + theData, updatePage); 
1

除了上面的答案,你可能會想嘗試$.getJSON功能,使事情更容易/更短。