admin管理员组文章数量:1405636
I'm trying to send a request that origins from an object that contains null
values:
ajax_post("path", {name: "Name", status: null}, callback);
I have my own method that URL encodes the objects. null
is a javascript type object
, so it ends up here:
for(var i in data) {
...
if(typeof data[i]=="object") {
if(data[i]==null) {
//HOW TO ENCODE THE NULL VALUE?
}
else
...
}
else
...
}
The resulting $_POST
on server side should contain these:
array(
name => "Name",
status => NULL
)
Is this possible or will I have to convert "NULL"
into NULL
manually on server side?
I'm trying to send a request that origins from an object that contains null
values:
ajax_post("path", {name: "Name", status: null}, callback);
I have my own method that URL encodes the objects. null
is a javascript type object
, so it ends up here:
for(var i in data) {
...
if(typeof data[i]=="object") {
if(data[i]==null) {
//HOW TO ENCODE THE NULL VALUE?
}
else
...
}
else
...
}
The resulting $_POST
on server side should contain these:
array(
name => "Name",
status => NULL
)
Is this possible or will I have to convert "NULL"
into NULL
manually on server side?
-
so you want to preserve the data type
null
when passsed on the query string? so that when the PHP receives it, the data type is stillnull
? – Kevin Commented Oct 2, 2014 at 10:56 -
1
Exactly as you say it. Basically it's a question whether
null
is in URL specification or if I'll have to create my own protocol/syntax for it. – Tomáš Zato Commented Oct 2, 2014 at 10:58 -
1
When you receive
$_POST
vars, the first thing you usually do is usingisset()
. I'd leave outnull
values and assume!isset == null
. Like:if (!isset($_POST['status'])) { $status = null; }
– Daniel W. Commented Oct 2, 2014 at 10:58 -
The problem in my case is, that I'm using the
null
field to actually set something to null. So it would be most straightforward to keep the value. Also, I use the key to detect what request I'm handling. Otherwise, you're right. – Tomáš Zato Commented Oct 2, 2014 at 11:00 -
1
@TomášZato: Maybe you could use the empty string instead of
NULL
– Bergi Commented Oct 2, 2014 at 11:06
4 Answers
Reset to default 1You could simply not send the parameter that is to be null.
In the backend you have probably some clause that evaluates if the Parameter exists.
If the parameter does not exist you have your null
For your example:
for(var i in data) {
...
if(typeof data[i]=="object") {
if(data[i]==null) {
//Dont send parameter so backend evaluates this to null
//unset(data[i]);
} else
...
} else {
...
}
}
You can use prefixes to differentiate null v. non-null.
So, if the variable is null
, send "0", and if the variable is non-null, send "1". Then, when you receive a request:
- verify the parameter is set and is not an empty string,
- verify the first character is in the set of allowed values ('0' or '1'), and
- strip the first character from the string and switch on that character, interpreting the remainder of the string accordingly
In Javascript you may use \0
which encodes to %00
when using one of the encodeURI*
methods:
console.log(encodeURI("\0"));
In PHP this would result in:
string(1) ""
Notice the one byte, in parison to zero bytes for an 'real' empty string.
for(var i in data) {
...
if(typeof data[i]=="object") {
if((data[i]==NULL) || (empty(data[i]))) {
//HOW TO ENCODE THE NULL VALUE?
// echo base64_encode(data[i]);
// echo base64_decode(data[i]);
echo 'NULL';
}
else
...
}
else
...
}
Make sure all or NULL's are uppercase as-well. Else they will not be a valid NULL variable.
* Not Tested *
本文标签: javascriptCan I urlencode null when sending requestStack Overflow
版权声明:本文标题:javascript - Can I urlencode null when sending request? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744300568a2599561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论