2010-09-14 143 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功能,使事情更容易/更短。