Ember CLI Download Excel [Save As] - blob/binary format Error - ember.js

I'm making an AJAX POST request from ember-cli to django rest framework in order to enable user to download excel/xls file. However, I'm stuck into formating problem, the file pops up but the content of the xls is not in correct format.
Here's the code I use in the controller:
Ember.$.ajax({
type: "POST",
url: "http://api.dev.maspa.biz/api/v1/panel/catalog/export",
data: 'ids=' + ids,
success: function(data) {
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:application/vnd.ms-excel' + encodeURI(data);
hiddenElement.target = '_blank';
hiddenElement.download = 'export.xls';
hiddenElement.click();
},
})
Looking for your helpful reply.
Thanks

Have you thought about using an ember addon to handle this export for you? It could be more reliable than coding it yourself.
Check out this add-on - https://github.com/roofstock/ember-cli-data-export.

Related

Does the Glip chat API support image (photo) attachments?

I'm using the Glip API to post messages. I can post images from the Glip UI but I don't see an option to post images. Does anyone know how to do this?
Glip API: https://developer.ringcentral.com/api-docs/latest/index.html#!#RefGlipCreatePost
Glip recently launched the file upload API which can be used to attach images. You could also try it out using our API Explorer.
In case someone comes across this looking for a working example, here's what I did (using Node and the RingCentral SDK):
var RC = require('ringcentral');
var fs = require('fs');
var FormData = require('form-data');
// {login to Glip and generate the platform object (https://github.com/ringcentral/ringcentral-js)}
var formData = new FormData();
formData.append('attachment', fs.createReadStream('image.png'));
platform
.send({
method: 'POST',
url: '/glip/files',
body: formData,
query: {
groupId: '1234', // whatever group you want to post to
}
})
.then(function(){
console.log('file uploaded');
})
.catch(function(e){
console.log(e.message);
});

post data from ember js to python

iam using ember js with python and tornado server.
I want to send the json data from ember js to python.
here is what i have tried
App.MyleavesController = Ember.ObjectController.extend({
submitAction: function () {
data=[{fromdate:this.get("model.fromdate")}]
$.ajax({
type: "POST",
url: "/apply_leave",
data: JSON.stringify(data),
success: function ( data ) {
}
});
}
});
in my python it is
class LeaveFormHandler(tornado.web.RequestHandler):
def post(self):
print json.loads(data)
but i could not get data..
it says " global name 'data' is not defined"
am a newbie please help..
For a POST request, use json.loads(self.request.body). Arguments to post() are extracted from the url regex.

Using ember data to save multiple records via post

Does anyone know of any working examples of overriding ember-data's DS.Adapter to save all records at once?
I'd like to stringify my entire array of records and send them to my server. I don't have much flexibility on the server to use anything but the existing API which is just a perl CGI script. Any help in the right direction would be appreciated.
What I did was iterate over the ember-data models, serialized them, saved them into a json object that matched server requirements, and executed post request.
var identifiedResponse = []
this.store.filter(key, function (record) {
return !!record.get('name')
}).then(function(resp) {
identifiedResponse.push(item.serialize())
})
jQuery.ajax({
url: url,
type: "POST",
data: identifiedResponse,
contentType: "application/json",
dataType: 'json',
xhrFields: {
withCredentials: true
}
})

Portable way of making an ajax request from JS code

This is probably a silly question, but I can't find an obvious response.
I'm developing a web application using (Geo)Django for the backend and Leaflet, among others, for the frontend. My point is I want to make a url call from my JS code to my Django backend. Something like this:
$.ajax({
type: 'POST',
url: '<A_URL_HERE>',
data: {"data":<MY_JSON_DATA>},
dataType: 'json',
success: function(responseData, textStatus, jqXHR) {
console.log("Data saved");
},
error: function (responseData, textStatus, errorThrown) {
console.log("Problem saving the data");
}
});
My problem is with the url. I know that cool URIs don't change, but for example, if I want to keep different dev/test/prod environments without changing that parameter, how should I do it? In the backend part, I'm following good practices for this kind of problems, but I have short experience with JS and frontend.
Many thanks in advance
Two possible solutions:
Use dynamically generated JavaScript, i.e. the JavaScript file is not a hardcoded static file. Instead it is a template which backend populates with variables (in particular URL);
Similar to the one above except that JavaScript is a hardcoded static file and for example you put your URLs in base HTML (which is generated from a template on the server side):
base.html
<script>window.urls = { "my_url": "{{ my_url }}" };</script>
my_script.js
$.ajax({
url: window.urls.my_url,
...
});
With this you can generate URLs based on you server settings.

Access Liferay Web services using JSON API

Hi i created a plugin portlet. In the JSP i am accessing all countries list by using JSON API. It is working fine for Logged in users. But for the Guest users i am unable to access the web service. I am working on Liferay 6.0.6. The following is my code.
Liferay.Service.Portal.Country.getCountries(
{},
function(result) {
for(var count=0;count< result.length;count++){
alert(result[count].name);
var option = document.createElement("option");
}
}
);
Assuming that you are using Liferay 6.1, you can achieve it by adding a property to portal-ext.properties file
json.service.public.methods=getCountries
If you need to check the whole flow checkout
JSONServiceAction
I think you need to pass the serviceContext with permissions to the Service.
Can you try by setting the communityPermissions and guestPermissions as VIEW ?
Liferay.Service.Portal.Country.getCountries(
{
serviceContext: jQuery.toJSON(
{
communityPermissions: communityPermission,
guestPermissions: guestPermission,
scopeGroupId: themeDisplay.getScopeGroupId()
}
)
},
function(result) {
for(var count=0;count< result.length;count++){
alert(result[count].name);
var option = document.createElement("option");
}
}
);
I found a work around for the above problem. I am unable to access JSON API because Liferay is using A.io.request for AJAX Calls which is available for Logged in Users only. So I have prepared the following code.
jQuery.ajax({
type: "POST",
url: '<%=themeDisplay.getURLPortal() %>'+'/tunnel-web/json?serviceClassName=com.liferay.portal.service.CountryServiceUtil&serviceMethodName=getCountries',
dataType: 'json',
success: function(countriesList) {
alert(countriesList);
alert(countriesList[0].countryId);
}
}
});