Alfresco Rules and Ajax - javascript

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");

Related

S3 static website that makes a POST request

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

How can I pass data from client to server use raw AJAX?

Hello I have already search but they often use jquery ajax to pass data from js to PHP(server-side). but for my project it has a bunch of pure js code so I should use raw AJAX to pass data.
For example, if I want to send a variable "Imgname" that value = 13 and want to echo in php page.
this is my try
<script>
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState === 4 && xmlhttp.status === 200) {
alert('send to server successfully');
}
};
xmlhttp.open("POST", "test2.php", true);
xmlhttp.send("Imgname=13");
}
</script>
in test2.php
<?php
$temp = $_POST['Imgname'];
echo $temp; /////output should be 13
?>
but error Undefined index: Imgname in C:\xampp\htdocs\test2.php on line 2
You need to make sure that you're sending the correct content-type:
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
Try sending the header:
var http = new XMLHttpRequest();
var url = "test2.php";
var params = "Imgname=13";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);

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.

XMLHttpRequest no return value

i am unable to receive a response from the php file.
Javascript
var http = new XMLHttpRequest();
var url = "http://localhost/php_bollywood_romance.php";
var params = "film="+movie_name_entered;
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() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
I have even tried to do this.....
var http = new XMLHttpRequest();
if ("withCredentials" in http)
{
var url = "http://localhost/php_bollywood_romance.php";
var movie_name_entered="hi";
var params = "film="+movie_name_entered;
http.open("POST", url, true);
}
else if (typeof XDomainRequest != "undefined"){
http = new XDomainRequest();
var url = "http://localhost/php_bollywood_romance.php";
var movie_name_entered="hi";
var params = "film="+movie_name_entered;
http.open("POST", url);
} else {
http = null;
}
if (http){
http.onload = function(){
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
document.write(http.responseText);
callback(http.responseText);
alert("hey");
}
}
http.send(params);
};
PHP
<?php
{
$name = $_POST["film"];
$file ='data_bollywood_romance.txt';
$current=file_get_contents($file); //gets existing content from the file
$current ="$current \n $name \n";
file_put_contents($file,$current); //put newstuff
echo 'hi';
return 'yes';
}
?>
However this also does not seem to work... if i do alert("http.response") after http.send(params)...it alerts an alert with nothing written on it. I thought this might be because there is same origin policy problem so i tried to do Cross-domain Ajax with Cross-Origin Resource Sharing but that didnt work either( that is the second code). Thanks for your help in advance.
You cannot set this two headers:
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
Previous thread about your problem
I tried it without and worked fine.
Personally i would go all the way using jQuery Ajax, but thats just "lazy me" :)

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().

Categories