Not able to get window.location.search value in HTML - jQueryMobile - node.js

My javascript code looks like this
var query = window.location.search.replace('?','');
fetchContent(apiUrl + 'service/' + query + '?callback=?', function(data) {
$('.content').append(data).trigger('create');
});
I am using jQueryMobile, nodeJS for services and views. Also, using EJS templates to display data.
The problem is that I am not able to get value for variable query in the above code and hence the next following lines form up a parameter 'apiUrl/service/?callback=?' for fetchContent method which is a wrong URL for my service. The correct parameter for my fetchContent method should be 'apiUrl/service/1234?callback=?'.
Interesting thing is that this code works fine when I open the link in new tab. URL for the above HTML page in my code is some.html?1234. According to this the value in JS code should be 1234 but it is empty.

Related

Sending an excel to frontend - Angular 2, Java

I have a Java API endpoint that returns an excel (I am using content-disposition=Content-Disposition","attachment; filename=Audit.Report.xlsx).
I have another Angular-Node JS application that needs to consume this API and when the user clicks on a link, it should pull the excel and display a pop-up asking them the location to save the document. I am at a loss as to how I can do this. I tried doing the following on the server side though,
Server Code:
getAuditReport = function ( req, resp ) {
var numberOfMonths = req.query.numberOfMonths;
console.log('In first method ' + numberOfMonths);
var auditReportPromise = this.getAuditReportXlPromise ( numberOfMonths );
auditReportPromise.then ( function ( data ) {
resp.headers('Content-Disposition: attachment; filename="audit.report_'+ new Date() + '".xls"');
resp.ContentType = "application/vnd.ms-excel";
resp.status ( 200 ).send ( data );
} ).catch ( function ( err ) {
resp.status ( 500 ).send ( err );
} );
}
The getAuditReportXlPromise method returns a promise to invoke the get method of the Java API. On invoking this API via a browser, I get the excel content on the browser rather than a prompt requesting me to save the document somewhere.
Can someone suggest what's wrong here, and what I need to do on the client side for the click functionality to work.
Update 1:
Following is the code from the HTML
<a id='10051' href="{{url}}" target=_blank class="ok-white-text">
Download Report
</a>
Based on the duration the user selects, I'm building the URL - this is getting built correctly.
If your API provides the file using GET then you should be able to download the file in a simple way by just setting a link to that API (i.e. an anchor containing the link like Download).
In this scenario the browser will take over the download process (i.e. locating the folder to download to, if configured to do so).
If you want to process the file on client side, then you need to transfer it in a binary mode as suggested by multiple answer for this question.
To answer how I did manage to get around this,
Client Side:
In the component, I did the following - wrote a method on clicking the button that contains a window.open to the url - something like,
window.open("<path to the java api>", '_blank');
The link like I mentioned above was a java api that was already generating the excel file.

Get wrong parameter from get method in nodejs

I'm trying to send get method request and want to pass value in URL.
Like my api look like
app.get('/api/getlocation/:customerName', customer.getlocation);
For call this I wrote in postman
localhost:8080/api/getlocation/:customerName=kumbhani
For test
var customerName = req.params.customerName;
console.log('name', customerName); // =kumbhani
It returns name with = sign - I want only kumbhani
The colon character in the path in Express has a special meaning: whatever you put in the URL after getLocation/ will be put in req.params.customerName.
This means in Postman, you should actually call this URL:
localhost:8080/api/getlocation/kumbhani
→ See related question.

casperjs empy POST data when large values are submitted

I have a form that is loaded and I just simply submit the form. In the form there is input type=hidden field that has some long string stored in it. This works fine on a regular browser and does not work with casper. On analyzing this with, apache itself is getting empty POST data from casper. If I reduce the data on the hidden input it works fine. Is there a size limit or something defined in casper?
Below is the code:
var casper = require('casper').create();
casper.start('http://localhost/loadForm', function() {
// Wait for the page to be loaded
this.waitForSelector('form[action="/saveConfig"]');
});
casper.then(function() {
this.evaluate(function() {
$('#form').submit();
});
});
casper.run();
The below bug report is what that helped me. I think this is a phantomjs bug. One of the hidden fields was storing a base64 png image and in my html page it was filled by canvas.toDataURL("image/png"). This in casperjs produces a different base64 compared to actual browers. This resulted in $_POST being empty in php. But when I tried file_get_contents("php://input") the data was all present. I solved it by using canvas.toDataURL("image/png", 0). The second argument produces consistent output in both browers and casperjs.
https://github.com/ariya/phantomjs/issues/10455

In facebook select date range insights of the page using nodejs

In nodes I am using the FB module for getting Graph API for getting insights on the page. For that i am giving request like
EX: https://graph.facebook.com/v2.2/12345_12345/insights?since=2014-12-01&until=2014-12-31
And I tried to manually hit in browser like
EX: https://graph.facebook.com/v2.2/12345_12345/insights?since=2014-12-01&until=2014-12-31&access_token=MYACCESSTOKEN
I refer following link and based on the instruction I tried some request, but I am getting only empty data even it has a data.
select date range to get insight of the page || get insights data of facebook page
https://developers.facebook.com/docs/graph-api/reference/v2.2/insights
https://www.facebook.com/help/336893449723054
NOTE: 12345_12345 is page.data.id value here I gave duplicate value.
This cannot work like this:
https://graph.facebook.com/v2.2/posts.data.id/insights?since=2014-12-01&until=2014-12-31
To build the URL out of a variable (that's what I guess you want to do), you have to contruct it like this:
var pageId = posts.data.id;
var reqUrl = 'https://graph.facebook.com/v2.2/' + pageId + '/insights?since=2014-12-01&until=2014-12-31';
...do http get request with reqUrl...

Assign jade variable to Angular

In my application, I'm using NodeJS, Express in the backend and Angular in the frontend. I'm also using Jade template engine.
jade obtains a variable called "adv" by this code.
res.render('page',{adv:result[0]})
In controller.js (for angular)
$scope.content = [];
I would like to do something like
form(ng-init="content=#{adv}")
h5 {{"content" + content}}
i.e. assign that jade variable to the scope. It is not working. I could use http.get and get the content directly to angular scope, but just wondering if it is possible to do this
Thanks
This worked.
- var str_adv = JSON.stringify(adv)
form(ng-init="content = #{str_adv}")
or even (where getContent is a function defined in controller doing the same thing)
form(ng-init="getContent(#{str_adv})")
Both these also parsed the string and stored the object in 'content'
But directly using
form(ng-init="content = JSON.stringify(#{adv})")
gives the same error
It seems assigning objects in ng-init is not possible.
Thanks #ivarni for the hint about stringify

Resources