1

For some reason I'm getting "SyntaxError: invalid label" error when I receive JSON data (see below) from a django function. Any ideas?

{ "id": "325", "from_date": "09-19-2011", "to_date": "09-20-2011" }

This is the jQuery code I'm using:

$(".edit_rec").click(function () {
    var rec_id = $(this).attr('name');
    $.post("/edit/", {
        editid: rec_id
    }, function (json) {
        var content = $.parseJSON(json);
        var to = new String(content.to_date);
        var from = new String(content.from_date);
    });
});
3
  • What's the reason for using new String? And is this the complete response? Commented Sep 19, 2011 at 13:45
  • When I click on the link (edit_rec) I first get a window saying "parsererror" then "SyntaxError: invalid label" and then "{ "id": "325", "from_date": "09-19-2011", "to_date": "09-20-2011" }". "String" I'm not sure if is necessary since the values come already as strings. Commented Sep 19, 2011 at 13:56
  • Removed the "new String" from my code. Same error "SyntaxError: invalid label" Commented Sep 19, 2011 at 14:01

1 Answer 1

2

you need to add "json" after the callback to let jquery know that the return data should be json. jQuery will then automatically parse your json string into a JavaScript object.

$(".edit_rec").click(function () {
    var rec_id = $(this).attr('name');
    $.post("/edit/", {
        editid: rec_id
    }, function (content) {
        var to = new String(content.to_date);
        var from = new String(content.from_date);
    },"json");
});
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer! It's worth asking for directions once in a while. It's said that we man don't like to do that :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.