1

I have an array of objects that I am trying to send to my PHP script. Before sending the array I can access all the data in it and everything is there. Once it gets to PHP var_dump returns NULL. I'm not quite sure how to send the data.

chrome.storage.local.get('object', function (object) {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            alert(xmlhttp.responseText);
        }
    }

    xmlhttp.open("POST", "http://example.com/php.php", true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    var uid = 2;

    JSON.stringify(object);
    xmlhttp.send("json=" + object + "&uid=" + uid);
});

The array:

var obj = [
    {
        "key": "val",
        "key2": "val2"
    },
    {
        "key": "val",
        "key2": "val2"
    }
]

obj.push({"key":val,"key2":val2});
chrome.storage.local.set({'object':obj});
0

1 Answer 1

3

This line:

JSON.stringify(object);

does nothing useful: you're throwing away the returned value from JSON.stringify(). Instead:

object = JSON.stringify(object);

will keep it around.

You really should be encoding your parameters too:

xmlhttp.send("json=" + encodeURIComponent(object) + "&uid=" + encodeURIComponent(uid));
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly, thank you. I can't believe I didn't notice 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.