I'm writing an application which uses AngularJS 1.4.0 which requires the ability to receive POST data from an external application. I know that routes in AngularJS often do parameters in the URL such as this:
.when('/section/:param', {
templateUrl: 'views/home.html',
controller: 'AppCtrl'
})
The problem with this style is that in my case the parameter is often very very long and it will cause the web server to ignore / truncate the request / parameter because the URL exceeds the maximum URL length.
For this reason, instead of a standard GET style parameter, I would like the application to receive a POST parameter, but I am not sure how to capture the parameter(s) and value(s).
Is there a way for Angular to capture the POST parameters directly? My second option would be to simply have an ng-init which uses a backend to grab the values, but I'd prefer to keep the parameters solely in Angular if possible. Thanks!
Except if your willing to do some weird black magic by setting cookies server-side - or something similar - there is no way to this in javascript.
POST values are sent to the server upon request, it's impossible capture these with javascript running in your browser.
Check out this answer aswell: How to read the post request parameters using javascript
Related
I am currently creating a search function in AngularJS 1. I am using Restangular in order to communicate between my JS frontend and an API.
I was wondering however, does AngularJS or Restangular in particular, strip out certain characters from my request?
I am doing a get request as follows using Restangular:
Restangular.all('details').get(detail_id).then(function(detailResult)
{ // etc...
However this works if detail_id is set to: 2019
but when detail_id is set to: 2019#1
Then Restangular (I assume) automatically strips off the #1 and I don't see this on the server side.
Am I making a mistake here or is this a limitation to Get requests with Restangular or alternatively is this working as intended and I have missed something?
Any help is appreciated,
Thanks
This is by design. The part after the # is called the fragment, and is it never sent to the server since it is supposed to be used on the client side only.
Per Wikipedia's article on the subject:
The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the web server
If # is actually part of the id, you'd need to url encode the parameter like this:
Restangular.all('details').get(encodeURIComponent(detail_id)).then(function(detailResult) { // etc...
In IIS, my web app runs in an application under the website. So instead of being accessible at www.example.com, it is actually at www.example.com/somepath/.
If I use window.location.pathname, I can get my hands on the /somepath/ portion, but with Angular's $location.path() I get nothing. From the official documentation, it seems like $location.path() should only get whatever comes after the hash, e.g. www.example.com/somepath/#/nextbit will give me /nextbit.
So how do I get my hands on the actual path where the website is hosted?
As you want to do it using Angular $location service, try this:
var baseUrl = $location.absUrl().replace('#/' + $location.url(), '');
absUrl() will get the whole URL, while url() will get the part of the url after #/
Since the information coming before the hash is not considered superbly relevant to Angular's internal workings, I don't think bypassing Angular's internal method ($location) would be a bad practice in this case.
I would go with your original assumption, since it will still work:
location.pathname
Unless you're worried about the bindings inside of Angular having fired prior to getting that path (which shouldn't have an affect on a static, IIS application path), this should be fine.
I have an web site (textnook) where users can filter books by author name , publisher , etc. On applying filters earlier I was using javascript to build the url and render the request and it looked something like /book/search/?author_id=1 Can I use angular 2 route to make the url readable while passing the parameters. Is it possible do it like a single page application?
Can I use angular 2 route to make the url readable while passing the parameters. Is it possible do it like a single page application?
Yes. ?author_id can be configured as a query parameter : https://angular.io/docs/ts/latest/guide/router.html
From the link it will need to be ; and not &/?:
Definitely, that is what Angular the router are for. Single Page Application with readable bookmarkable URLs.
You can either encode parameters as "directory" or as query parameter.
Suppose I have a single page application that makes AJAX requests to http://www.somesite.com/resources?lang=en.
Now, let's say that my application has a special variable for the request's languages, that is, if the variable's value is en, then the AJAX request will send a query param lang=en.
For now, the two possible values are en and es.
This variable's value can be changed dynamically by the user (clicking a button).
So, if we are using the application with language = en, then the application will send the lang=en query parameter. If the user changes the language to es, then I want the application to resend all the AJAX requests but now with lang=es query parameter.
It would be like refreshing the website, but asynchronously via AJAX requests.
I have been thinking this yesterday and I can't find a way to handle this logic in an AngularJS application.
I'm not an expert in AngularJS, but I understand the concepts of modules, directives, services and controllers.
What would be the best approach to handle this scenario? That is, I would like to avoid having to call each AJAX request manually, I'd rather create an automatic way of doing this.
Have you tried any AngularJS plugins?
First that comes to mind is Angular translate
Best way to concate the ajax url with lang parameter and request again.
So you will get all new data base on lang parameter, won't forget to handle cache before requesting the ajax call.
I want to post some data via javascript to another domain. Something like:
http://www.othersite.com/submitfunnyname?name=blah
The other site (othersite.com) has a REST interface that you can call (well actually this is a get example) to submit a funny name to them.
Can I do this already with javascript? I'm a little confused on this - I know if that service wants to return some data, I'd need to use something like JSON-P - even though here I'm submitting some data, I guess the service will return some message structure letting me know the result, so it would have to be JSON-P, right?
Thanks
Not a particular expert in JavaScript, but isn't this an example of "cross-site scripting", which is not allowed due to possible security threats?
I believe you need to have all HTTP calls being made to the same server domain as the page. You could have a handler on your own site pass the information on to the othersite.com.
You can either use JSON-P if the site supports it, or you can use your web server as a proxy - by making requests to your server, which will in turn use a library such as cURL to make the actual request to the remote site.