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?

Share Improve this question asked Oct 2, 2014 at 10:53 Tomáš ZatoTomáš Zato 53.5k63 gold badges310 silver badges828 bronze badges 7
  • 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 still null? – 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 using isset(). I'd leave out null 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
 |  Show 2 more ments

4 Answers 4

Reset to default 1

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

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