Home > Enterprise >  '[]' appended to the end of my post request parameter in javascript
'[]' appended to the end of my post request parameter in javascript

Time:07-21

I am trying to make post request with the following parameters:

const postParams = { 'submission': submission };

However, when the request is actually sent, I observe that submission becomes submission[] in the browser's network tab. This is problematic because my server expects submission and not submission[]. Check the image below for more information.

[Note that the [] was appended to submission][1] [1]: https://i.stack.imgur.com/pXpNq.png

Note that submission (the variable) is an array of integers, so while it makes sense why the [] is attached, is there a way to get rid of that?

Below is the full function.

    const postParams = {  submission: str_sub };
    console.log(postParams)
    $.post(root '/problems/multiple_choice/' problem_pk '/run',
            postParams,
            function(data) {
               // Omitted / not relevant
            })
     .fail(function(jqXHR, textStatus, errorThrown) { console.log(textStatus); });

CodePudding user response:

This is jQuery's default behaviour where it follows PHP (instead of traditional) conventions.

See the documentation.

Use .ajax instead of .post and include traditional: true in the options object.

$.ajax(
    root '/problems/multiple_choice/' problem_pk '/run',
    {
        data: postParams,
        traditional: true,
        success: (data) => { ... }
    }(    
  • Related