jQuery - parseJSON不是一个函数?

时间:2010-09-14 14:41:29

标签: jquery

我正在使用Wordpress 3和jQuery 1.4.2。 Firebug告诉我$ .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));  
        }
    })

});

});

3 个答案:

答案 0 :(得分:2)

我不确定为什么会收到该错误消息,但我认为这不是真正的问题。

您正在调用updatePage函数,而不是将其放在对象中。删除名称后面的括号:

success: updatePage,

当您在执行AJAX调用之前调用该函数,并且没有任何参数时,您尝试解析的theResponse参数未定义。

此外,当您在AJAX调用中使用json数据类型时,您根本不需要解析数据,这已经在调用成功回调函数时完成。

答案 1 :(得分:1)

您在dataType:'json'来电中添加了$.ajax。这意味着jQuery将为您解析JSON。你也为什么两次打电话给$.parseJSON

此外,像Dick说你可以使用$.getJSON简写。

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

答案 2 :(得分:1)

除了上面的答案,你可能想尝试$.getJSON功能,让事情变得更容易/更短。

相关问题