Posting key value pair as individual data Ajax
<p>I am building a function which posts data using Ajax to my Codeigniter controller. The problem is as follows.</p>

<p>Codeigniter requires individual post data (no object) to allow me to fully validate the data. Otherwise, I need to use the validation rules as discrete methods, and some rules, like xss_clean, are not available as discrete methods (see <a href="https://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference" rel="nofollow">CI docs</a>). I therefore need to send the data in Ajax like this.</p>

<pre><code>data: {
    username: username,
    password: password
},
</code></pre>

<p>I now have an array passed to the post function.</p>

<p><code>{'username': $("#username").val(), 'password': $("#password").val()};</code></p>

<p>I need to rebuild this array to the Ajax data way above. I tried for loops and the <code>.each()</code> function in the Ajax POST function, but this is not working.</p>

<p>Anyone any ideas on how to fix this and allow me to pass an array to the post function, but also allow me to separate this data in order to validate it with Codeigniter.</p>