Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I urlencode null when sending request?

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?

like image 920
Tomáš Zato - Reinstate Monica Avatar asked Oct 02 '14 10:10

Tomáš Zato - Reinstate Monica


People also ask

Is UrlEncode necessary?

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.

What do you use the function UrlEncode for?

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.

What is %2C in URL encoding?

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.


1 Answers

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 {
    ...
  }
}

like image 103
Jannis Avatar answered Nov 04 '22 01:11

Jannis