3

Trying to go back to basics here and send a JSON object via ajax to my php. I can't even get to that part as I get a JSON error. Here is the jquery code:

jQuery(".deletebutton").on("click", function() {

        var employees = [
        { "firstName":"John" , "lastName":"Doe" },
        { "firstName":"Anna" , "lastName":"Smith" },
        { "firstName":"Peter" , "lastName": "Jones" }
        ];

        var dataString = JSON.stringify(employees);

        // Lets put our stringified json into a variable for posting
        var postArray = {json:dataString};

        jQuery.ajax({
            type: 'POST',
            url: 'index.php?option=com_recordings&task=deletevideos&format=raw',
            data: postArray,
            dataType: 'json',
            success: function(data){
                if (data == "blah")
                    alert(data);

            }
        });
});

I get this error (when I check errorThrown): SyntaxError: JSON.parse: unexpected character. I checked with jsonlint.com that it is valid JSON. What am I doing wrong?

0

3 Answers 3

3

dataType refers to the request header, not the response. If you are not sending back valid JSON, jQuery won't like it. You want to send JSON, but you probably want to get back something else. Just remove dataType and it should work properly, unless there's an error on the server script.

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

Comments

1

The thing you are posting is a js-object not json. You have to post the stringified json.

data: dataString

Comments

1

change to data: dataString

and no problems will occure

Comments

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.