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?
Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.
The urlencode() function is an inbuilt function in PHP which is used to encode the url. This function returns a string which consist all non-alphanumeric characters except -_. and replace by the percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.
The %2C means , comma in URL. when you add the String "abc,defg" in the url as parameter then that comma in the string which is abc , defg is changed to abc%2Cdefg . There is no need to worry about it.
You 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 {
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With