简体   繁体   中英

How to preserve NULL values in jQuery ajax POST sent to PHP script?

Suppose I have a PlainObject:

formData: completed: null limit: "10"

Which I then use in an ajax call:

$.ajax({
  type: 'post',
  url: SomeEndpoint.php,
  data: formData,
}).done(function (response) { do stuff }

The POST value in SomeEndpoint.php receives all values as empty strings:

$completed = $_POST['completed'] ?? null; // ""
$limit = $_POST['limit'] ?? null; // "10"

How can I preserve the typing? What's the best way to validate typing if an endpoint validates that a variable is either NULL or an Int/Bool?

You can use jquery validation as validator - https://jqueryvalidation.org/

Options - https://jqueryvalidation.org/category/plugin/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM