title: Sending "windows-1251"-encoded string in JSON from python to javascript
body: <p>What I need to do is best descriped as example. 
  Previously, I had the following code:</p>
  
  <pre><code>content = u'&lt;?xml version="1.0" encoding="windows-1251"?&gt;\n' + ... #
  with open(file_name, 'w') as f:
       f.write(content.encode('cp1251'))
       f.close;
  </code></pre>
  
  <p>Now I want to modify the architecture of my entire app and send the string which is supposed to be the file content to client via JSON and to generate the file via javascript. </p>
  
  <p>So, now my code looks something like this:</p>
  
  <pre><code>response_data = {}
  response_data['file_content'] = content.encode('cp1251')
  response_data['file_name'] = file_name    
  return JsonResponse({'content':json.dumps(response_data,  ensure_ascii=False)}) # error generated
  </code></pre>
  
  <p>The problem is that I get <code>UnicodeDecodeError: 'ascii' codec can't decode byte 0xd4 in position 53: ordinal not in range(128)</code></p>
  
  <p>I also tried the second option this way:</p>
  
  <pre><code>response_data = {}
  response_data['file_content'] = content
  response_data['file_name'] = file_name    
  return JsonResponse({'content':json.dumps(response_data,  ensure_ascii=False).encode('utf8')}) # error generated
  </code></pre>
  
  <p>Then, on client, I try to covert utf8 to windows-1251.</p>
  
  <pre><code> $.post ('/my_url/', data, function(response) {
          var file_content = JSON.parse(response.content).file_content;
          file_content = UnicodeToWin1251(file_content);
  </code></pre>
  
  <p>...but...I get distorted symbols. 
  I know I am doing something terribly wrong here and am likely to mess up things with encoding, but still it's been an entire day I couldn't solve this issue. Could someone give a hint where my mistake is ?</p>