We are using window.location() to download files from server. We are sending a JSON string along with request. As this being a GET request, I am forced to have restriction on the length of the JSON string. How can I send more data with window.location? sort of POST request to download the file.
Sounds like you want to download a file using POST method..?
One method is to create a hidden form with post method and submit it using javascript. Then the server has to respond back with a content-disposition header to download the file.
If you can't add this header, then you have to send a post request using ajax and then save the response you get back using version client-side technics like: FileSaver or StreamSaver
Related
I would like to send data using the POST method, have the body be a JSON object rather than a set of key-value pairs, and on success navigate to the response.
If I just wanted to send data with POST and navigate to the response, I would use a form. If I just wanted to send a JSON with POST, I could use AJAX. But is there any way to fit both criteria?
Not really. The closest options you are have are to use Ajax and then:
Use history.pushState to change the URL and replace the entire DOM with the response.
Have the server store the result somewhere and then return a URL in the response that the JavaScript could then assign to location.href. The URL would have a token that the browser could use to get the stored response.
I don't think there's any way to literally do what you've described.
If you're posting to a URL in the same origin (or CORS is enabled), you could do the post via XMLHttpRequest, use history.pushState to change the browser's URL to the target, and update the page's DOM with the response from the post.
But the only way to literally do what you describe is with a form, and forms can't currently send JSON.
I have a cms, where am using laravel as web api, angularjs for requests.
I have an iframe where I call to services with a direct link and put it usig trusted src function.
The main problem is, I can not use a normal http post request and hide parameters, because using http request will return data, not file, and the report api returns in headers, an html file, pdf ... etc) so when i get result to the success of my http request, it won't download pdf file, it will show special chars
in the i frame am calling the api like this :
"localhost/api/getreportService/"+$scope.brandid+"&"+$scope.customerid"
but that's cannot be secure, is there any way to hide the request here from users?
ok, I found a solution, I called the api via http post request then I used $sce tustAsHtml for the response, with a ng-bind-html in my template and the result is good now, the report is showing in the div,
Now all is safe, the user needs a token to access the report, and that's impossible without a login.
I'm making an GET request to my server with AJAX+JS. I'm using it to delete file like this:
delete.php?file_id=0123456789&user=555555
When I send GET request delete.php will delte file with ID 0123456789, but is there a way to accept only request that server makes to itself.
For example if user opens new tab and types www.mysite.com/delete.php?file_id=0123456789 server will decline that request, but if I call it with JS function server will accept the request.
How about using X-XSRF-TOKEN
in combination with Angular JS?
This will require your webapplication to generate and check this token, but your AJAX request will be authenticated.
How can I download an XML file when I call a REST endpoint (which responds in XML format) with AngularJS?
So the flow is simple, I have created a button on the UI which makes a call to a REST endpoint (which has a response in an application/xml format) and its response should come as a download on the UI.
If I understand the question correctly (use the browser's download functionality when the user clicks the link), then this is not a question about angular really.
In order to cause a file to download you need to do two things:
Attempt to navigate the browser to the URL that returns the XML (i.e. don't make an AJAX request for it).
e.g. <a href="http://myserver.com/my/REST/endpoint>Click here</a>
Ensure the XML content is being served with headers that would force a download. If you don't do this, the browser may attempt to render the XML itself rather than downloading it. You could try either setting the Content-Type header to be applicaton/octet-stream or look into using the Content-Disposition header:
Content-Disposition: attachment; filename=someFileName.xml;
I want to create a very primitive mixpanel/kissmetrics/analytics clone.
Therefor I need to send data to a remote website for the purpose of logging it. It should send simple parameters like, foo=bar, foo2=bar2
How can I send data with javascript without being caught to cross domain policy?
Can you provide an example code?
Javascript can send arbitrary HTTP requests to any domain you like; you just cannot read the response.
To send a GET request, create an <img> element and set its src.
To send a POST request, create a hidden <form> that submits to a hidden <iframe>, then submit it.