Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handling csv response in backbonejs

I am posting some data to server through Backbone.js and server sends a csv file as response. As Backbone.js handles only json format can some body tell me how to handle this case, so that i would be able to download the csv file gracefully.

  object = {};
  object.c1 = formObj.c1
  hash = {
    success: function(model, response, options) {

    },
    error: function(model, response, options) {
      return console.log(response);
    }
  };
  model = new P.models.mine(object);
  model.doSomething(object, hash);

It always comes to error part.

like image 929
Trying Avatar asked Nov 18 '25 20:11

Trying


1 Answers

The ideal way to handle this would be to change your back end code to return JSON, or create another route that returns JSON. Since you are asking this question I'm assuming that isn't an option for you.

Basically you are going to have to parse the CSV on the client side:

https://stackoverflow.com/a/1293163/944006 - Should get you started.

If you are asking to download a csv file, then just pointing the browser at the location should prompt the user for download. You cannot prompt a file download through ajax(for good reason), but there are ways to tiptoe around this limitation:

https://stackoverflow.com/a/9970672/944006

like image 199
Thad Blankenship Avatar answered Nov 20 '25 12:11

Thad Blankenship



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!