I do the Ajax request like this:

$("document").ready(function() { $(".js-ajax-php-json").submit(function() { var data = { "action": "test" }; data = $(this).serialize() + "&" + $.param(data); $.ajax({ type: "POST", dataType: "json", url: "response.php", //Relative or absolute path to response.php file data: data, success: function(data) { $(".the-return").html( "Favorite beverage: " + data["favorite_beverage"] + "<br />Favorite restaurant: " + data["favorite_restaurant"] + "<br />Gender: " + data["gender"] + "<br />" + data["some"] + "<br />JSON: " + data["json"]); } }); return false; }); }); 

What kind of objects are var data = {"action": "test"}; and why associate them?

  • Use a four space indent or the {} button to format the code. - AivanF.
  • These are additional data, except for those that you serialized from the form. - Jean-Claude
  • about the design will take into account - Junior Coder
  • jean-claude why this data? - Junior Coder
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Hmm, and you can not ask the author of the code?

He apparently used to develop additional fields for debugging:
I'll make my AJAX-wrapper , with checks and an implicit substitution of a CSRF-токена ! Although why do I need to check?

Everything depends on where this code comes from, now we can only assume that it replaces the standard action on the server and production. It is better to remove this field.

    Actually serialization is not needed. JQuery can work with an object. The essence of the serialize() function is that it turns an object into a string. For example {'foo' : 'bar', 'ping' : 'pong'} in 'foo=bar&ping=pong' . data = {"action": "test"} is an additional object that is added to the general, expanding it with some optional values.

    In this situation, {"action": "test"} is hardly needed for something, and most likely written from a noodle, but you can add your own values ​​there if you have some kind of own API.