gpt4 book ai didi

php - 在 GET ajax 调用中传递 HEX 颜色

转载 作者:行者123 更新时间:2023-11-29 18:25:27 25 4
gpt4 key购买 nike

我正在尝试通过 GET ajax 调用将一些十六进制颜色传递给 php 脚本。 PHP 似乎不喜欢十六进制颜色,但我尝试替换 # 并使用 encodeURIComponent 两者都不起作用。

这是 js(每个 currentColors 条目都是十六进制颜色)

var dataString = 'designName=test&mc1='+currentColors[1]+'&mc0='+currentColors[0]+'&sp='+currentColors[2];
var strippedString = encodeURIComponent(dataString);

最佳答案

使用encodeURIComponent对URI组件进行编码:

var strippedString = 
"designName=test" +
"&mc1=" + encodeURIComponent(currentColors[1]) +
"&mc0=" + encodeURIComponent(currentColors[0]) +
"&sp=" + encodeURIComponent(currentColors[2]);

例子:

var strippedString = 
"designName=test" +
"&mc1=" + encodeURIComponent("#FF0000") +
"&mc0=" + encodeURIComponent("#00FF00") +
"&sp=" + encodeURIComponent("#0000FF");
// "designName=test&mc1=%23FF0000&mc0=%2300FF00&sp=%230000FF"

在服务器端,查询字符串将产生:

// parse_str("designName=test&mc1=%23FF0000&mc0=%2300FF00&sp=%230000FF", $my_GET);
// var_dump($my_GET);

array(4) {
["designName"]=>
string(4) "test"
["mc1"]=>
string(7) "#FF0000"
["mc0"]=>
string(7) "#00FF00"
["sp"]=>
string(7) "#0000FF"
}

关于php - 在 GET ajax 调用中传递 HEX 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13948832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com