S3 static website that makes a POST request - javascript

Is it possible to make a POSt request from a static website hosted on S3? Are there any workarounds?

Totally possible, just use XMLHttpRequest or add some lib (jquery) to help you with that:
<script>
var xhr = new XMLHttpRequest();
xhr.open("POST", '/server', true);
//Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
// Request finished. Do processing here.
}
}
xhr.send("foo=bar&lorem=ipsum");
// xhr.send(new Int8Array());
// xhr.send(document);
</script>
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send

Related

XMLHttpRequest not sent

We are three developers working on a web-app project with Django.
Some communications client-server use an API. One particular request is not sending on one of the developer's computer. We use the same Browser (Firefox quantum 64.0 (64 bits)), same OS (Ubuntu 18.04).
Here is the JS snippet:
function requestHandler(url){
let xhttp = new XMLHttpRequest();
xhttp.open('POST', url, true);
xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhttp.setRequestHeader('X-CSRFToken', getCookie('csrftoken'));
xhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
drawLogTable();
}
else if (this.readyState == 4){
console.log(this);
new PNotify({
title: 'Échec!',
text: "Opération impossible: " + this.statusText,
type: 'error'
});
}
}
xhttp.send();
}
Note that url is '../../api/log?action=accept&id=1'
On all the working computers and browsers, the request posts and does a 404 since server-side has no implementation yet. On the non-working computer, chromium works, but under Firefox, the request is not even sent (server log is empty) and the request object is completely empty (no status, no text).
We have other AJAX requests in the same type that are working fine.
Can you try this:
xhttp.onload = function(){
if (this.status == 200) {
drawLogTable();
}

Alfresco Rules and Ajax

I'm quite new to Alfresco and have been tasked with creating a rule that calls an external php script whenever a folder is created. There doesn't seem to be a lot of information out there that indicates that this is possible. I tried to create a script like the following, but I receive an "XMLHttpRequest not defined error":
function submitDoc(url, params){
var http = new XMLHttpRequest();
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
logger.system.out("SUCCESS!");
}
else{
logger.system.out("Failure: "+http.status);
}
}
http.send(params);
}
submitDoc("/addPage.php", "var1=somevar");

PHP not able to retrieve request.send

I am a novice in PHP and Javascript, and I am having some difficulties sending Google map bounds to a PHP that runs a SQL to find addresses within the bounds and generates an XML used to place markers.
I got some help to clean up my code, but the problem persists. The problem is either that my Javascript is not sending data to the PHP, or that the PHP is not reading the data in the proper manner.
PHP stops if I inlude this line (not actually being used yet in the PHP-file, other than this line:
$South = $_POST['South'];
Javascript function (North, South, East and West are defined and populated earlier in the js-file. Inserting alert(South) works, so I don't think that is the problem),
function downloadUrl(url, callback) {
var request = !window.XMLHttpRequest? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest; //changed this line to select the XMLHttpRequest by default and not use the activeX version when XMLHttpRequest is available.
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange;
callback(request, request.status);
}
};
request.open('POST', url, true);
request.send('South='+South+'&North='+North+'&West='+West+'&East='+East);
}
Add request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); after request.open('POST', url, true);
It should be like this:
request.open('POST', url, true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send('South='+South+'&North='+North+'&West='+West+'&East='+East);
And replace if (request.readyState == 4) { with
if (request.readyState == 4 && request.status == 200) {
Then it will work.

POST Request from Chrome Extension

I am trying to POST with my chrome extension.
Here is the method I am using for it.
function uploadFile(url){
var req = new XMLHTTPRequest();
req.open("POST", "https://wepicit.s3.amazonaws.com/", true);
var params = "key="+myKey
"&acl="+"public-read"
"&Content-Type="+"text/plain"
"&AWSAccessKeyId"+tempKey
"&file="+url+".txt"
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", params.length);
req.setRequestHeader("Connection", "close");
req.send(params);
req.onreadystatechange = function(){
// If the request completed, close the extension popup
if (req.readyState == 4)
if (req.status == 200) console.log('success');
};
}
The error that I am getting is that 'XMLHTTPRequest()' is not defined. Please help. Thank You.
JavaScript is case sensitive.
Not new XMLHTTPRequest() but rather new XMLHttpRequest().

get full html source code of page through ajax request through javascript

The javascript code will be launched from www.example.com through the url bar in google chrome so i cannot make use of jquery. My goal is to pass the full html source code of www.example.com/page.html to a variable in javascript when i launch the code in www.example.com. Is this possible? If so how? I know to get the current page source it's just document.documentElement.outerHTML but i'm not sure how i'd do this. I think it's possible by using responseText somewhere in the following code:
http.send(params);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://www.example.com/page.html",true);
xmlhttp.send();
data = ""
url = "http://www.example.com/page.html"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
data = xhr.responseText
}
}
xhr.send();
function process(){
url = "http://www.example.com/page.html"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
alert(xhr.responseText)
}
}
xhr.send();
}
this is how i run script from the address bar.. I do it all the time..
i create a bookmark like this
javascript:script=document.createElement('script');script.src='http://10.0.0.11/clear.js';document.getElementsByTagName('head')[0].appendChild(script); void(sss=1);
then i host the js file on my computer.. i use analogx simpleserver... then you can use a full page for your script

Categories