Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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); });

>Solution :

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) => { ... }
    }(    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading