我将以下 jquery 发布到 php 页面。 php 页面上的 var_dump($_POST);
返回 null,但如果我这样做:
$.post("test.php", {"script": script} );
一切正常。
我不想这样做的原因是因为数组可以有可变参数。有什么帮助吗?
var arr = new Array();
arr["script"] = scrip;
arr["account"] = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4b9b5ac94bcbba0b9b5bdb8fab7bbb9" rel="noreferrer noopener nofollow">[email protected]</a>";
arr["accounttag"] = "TH";
if (follow != "")
arr["followtag"] = follow;
if (join != null)
arr["join"] = join;
$.post("test.php", arr );
//{"script" : script, "account" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="503d312810383f243d31393c7e333f3d" rel="noreferrer noopener nofollow">[email protected]</a>", "accounttag" : "TH"}
//works if used instead of arr
///test.php
var_dump($_POST)
array 0{null};
最佳答案
你的var arr
应该是一个Object
而不是Array
。
尝试如下:
var data = {
script: scrip,
account: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fe2eef7cfe7e0fbe2eee6e3a1ece0e2" rel="noreferrer noopener nofollow">[email protected]</a>",
accounttag: "TH"
}
if (follow != "")
data.followtag = follow;
if (join != null)
data.join = join;
$.post("test.php", data);
参见http://api.jquery.com/jQuery.post/解释$.post
。
关于php - $.post() Jquery post 无法使用 new array() 到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17491702/