lundi 2 avril 2018

Unexpected token P in JSON at position 0 when trying to return Excel spreadhsheet

I have an emberJS application where I can make a POST AJAX call to a Django backend. A function in Django creates an xlsx file for a bunch of queried items based on IDs coming in the POST request. It goes through the Django view function without any issues, but when the HTTP response is returned to ember, I get the error

SyntaxError: Unexpected token P in JSON at position 0
    at parse (<anonymous>)
    at ajaxConvert (jquery.js:8787)
    at done (jquery.js:9255)
    at XMLHttpRequest.<anonymous> (jquery.js:9548)
    at XMLHttpRequest.nrWrapper (base-content:20)

I'm setting the response content type to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, so I'm unsure as to why its trying to read the response as JSON.

Python Code

file_path = '/User/path_to_spreadsheet/content.xlsx'
fsock = open(file_path, "rb")

response = HttpResponse(fsock, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename="content.xlsx"'
return response

EmberJS Code

export default Controller.extend({
  actions: {
    storeProductId(products) {
      let product_ids = []

      products.forEach(function(product){
        product_ids.push(product.id)
      });

      let adapter = this.store.adapterFor('product-export');
      adapter.export_products(product_ids).then(function(response){
        console.log(response)
      }).catch(function(response) {
        console.log('ERROR')
        console.log(response)
      })
    }
  }
});




Aucun commentaire:

Enregistrer un commentaire