我有一组对象要发送到我的 PHP 脚本。在发送数组之前,我可以访问其中的所有数据,一切都在那里。一旦到达 PHP var_dump 返回 NULL。我不太确定如何发送数据。
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);
});
数组:
var obj = [
{
"key": "val",
"key2": "val2"
},
{
"key": "val",
"key2": "val2"
}
]
obj.push({"key":val,"key2":val2});
chrome.storage.local.set({'object':obj});
最佳答案
这一行:
JSON.stringify(object);
没有任何用处:您正在丢弃 JSON.stringify()
的返回值。相反:
object = JSON.stringify(object);
会保留它。
你真的应该也对你的参数进行编码:
xmlhttp.send("json=" + encodeURIComponent(object) + "&uid=" + encodeURIComponent(uid));
关于javascript - 通过 POST 发送一个 JavaScript 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25652071/