0

I'm using Wordpress 3 and jQuery 1.4.2. Firebug tells me $.parseJSON is not a function and I'm stumped as to why.

Any suggestions are appreciated.

$(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));  
        }
    })

});

});

2
  • Look the API documentation api.jquery.com/jQuery.parseJSON, Commented Sep 14, 2010 at 14:46
  • 1
    In the second last line there's missing a semicolon. Adding that maybe that solves the problem... Commented Sep 14, 2010 at 14:46

3 Answers 3

2

I'm not sure why you get that error message, but I don't think that's the real problem.

You are calling the updatePage function instead of putting it in the object. Remove the parentheses after the name:

success: updatePage,

As you are calling the function before you do the AJAX call, and without any parameters, the theResponse parameter that you try to parse is undefined.

Also, as you are using the json data type in the AJAX call, you don't have to parse the data at all, that is already done when the success callback function is called.

Sign up to request clarification or add additional context in comments.

Comments

1

Aside from the answer above, you might want to try the $.getJSON function, make things easier/shorter.

Comments

1

You added dataType:'json' in the $.ajax call. This means that jQuery will parse the JSON for you. Also why do you call $.parseJSON twice?

Also, like Dick said you can use the $.getJSON shorthand.

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

Comments