Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send data as key - value pairs instead of string via POST using XHR
    primarykey
    data
    text
    <p>I'm creating two POST calls. One using a django form and one using angular js via a resource xhr.</p> <p>The angular setup looks like this:</p> <pre><code>myModule.factory('gridData', function($resource) { //define resource class var root = {{ root.pk }}; var csrf = '{{ csrf_token }}'; return $resource('{% url getJSON4SlickGrid root.pk %}:wpID/', {wpID:'@id'},{ get: {method:'GET', params:{}, isArray:true}, update:{method:'POST', headers: {'X-CSRFToken' : csrf }} }); }); </code></pre> <p>With creating an xhr post request as such:</p> <pre><code>item.$update(); </code></pre> <p>This post request is send to the server as expected, but when I want to access the <a href="https://docs.djangoproject.com/en/dev/ref/request-response/#querydict-objects" rel="nofollow noreferrer">QueryDict</a> I cannot access the data passed using:</p> <pre><code>name = request.POST.get('name', None) </code></pre> <p><code>name</code> is always <code>None</code> like this.</p> <p>The issue behind this is that the QueryDict object is getting parsed quite strange. </p> <pre><code> print request.POST &lt;QueryDict: {u'{"name":"name update","schedule":0"}':[u'']}&gt; </code></pre> <p>Whereas I would have expected this result, which I got when I send the data via a "normal" Post request:</p> <pre><code>&lt;QueryDict: {u'name': [u'name update'], u'schedule': [u'0']}&gt; </code></pre> <p>So it seems to be that Django receives something in the POST request which instructs Django to parse the parameters into one string. Any idea how to circumvent this?</p> <p>Update:</p> <p>I found <a href="https://stackoverflow.com/a/6322561/237690">this</a> discussion where they say that the issue is if you provide any content type other than MULTIPART_CONTENT the parameters will be parsed into one string. I checked the content-type send with the POST request and it is really set to <code>'CONTENT_TYPE': 'application/json;charset=UTF-8'</code>. Thus this is likely the issue. Therefore my question is: How can I set the CONTENT_TYPE for a xhr post request created using angular.js resources to <code>MULTIPART_CONTENT</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload