Call a rest webservice using javascript - javascript

So I created a server application using Java and wildfly that have multiple rest webservices. This webservice return xml and are working. This was the easy part, but now I have to make an application that is going to use the web services. This application is not a website and I have problems calling the web services. I tried
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "url, false);
xhttp.send();
This url is a valid url. When I try to run the code that call this javascrip I got the following error.
XMLHttpRequest' is undefined
At first i thought tha was some missing library (i am realy new to javascript), but when I google it, I understood that this method is only usable when it runs in a web page.
I don't want the code to do this I only want to know if there is a method I can use to call a rest web service and get the xml result. If possible a method native to javascrip (not using query or others) and that can be used in a aplication and not a web site.
Thank you for your help.

Related

Apache Server PHP works in address bar, but not when using AJAX from Javascript?

I have created an Apache server on an EC2 instance that I am using to host a mySQL database and some PHP scripts to access that database. I have installed what I think are the all of the required dependencies on the Apache server, to the point where when I write a PHP script and access it via a web browser using the address bar, the PHP script runs, queries my database and returns the correct information to the web browser.
However, the goal of the scripts is to be used by a Javascript application to access the database via AJAX, specifically a Captivate course. When I try to use AJAX to access the scripts, nothing happens.
Captivate gives very little information about error logging, so I have tried using other services to check my javascript to make sure it is working, such as jsfiddle.net, but it seems like the Javascript is not the problem. Here is my code:
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send();
alert(xmlHttp.responseText);
}
My goal is not necessarily to use Alert to post the code to the screen, I'm using it just to check if I got anything back in the first place. theUrl leads to a php script on the other end called simpleecho.php:
<?php
echo "test";
?>
I have more complicated PHP scripts, but they also all work in browser. If I go to theUrl in an address bar of a web browser, I see "test".
I have tried changing from Async requests to Sync, I am certain that Captivate's javascript can use jQuery related code, I have tried like 8 different ways of sending the AJAX request as both POST and GET, but I'm not sure what I am missing. My gut says there is something wrong configured with the Apache server that is keeping it from responding to specifically requests outside of my address bar, but I don't know enough about configuring my httpd.conf file to know what I am looking for.
Any and all tips and learning is appreciated!
The answer to this question was that I was not allowed to use HTTP to communicate with the apache server, and it has to be secured with SSL to use HTTPS. I tried using a Self-Signed certificate, but the self-signed version was also not allowed for use with a Captivate Course.

How can I create a different URL for every new Object

I have built this site "https://supsurvey.herokuapp.com/SurveyCreate.html"
You create a survey and then it redirects you to a unique URL
https://supsurvey.herokuapp.com/SurveyPage.html#718807c9-3a5b-4745-b953-511afef5e073
In the surveyCreate page I have location.assign (SurveyPage.html#${survey.id}) which is linked to SurveyPage.js and from there I extract the uuid4 using currentUrl.split (#) and then I send a get request to my server which is built in NODE-JavaScript (only for strong survey objects in MongoDB) for the correct survey OBJECT and display it to the user.
I want so so that after you press create Survey you will be redirected to
https://supsurvey.herokuapp.com/SurveyPage/718807c9-3a5b-4745-b953-511afef5e073
So instead of .html#{uuid} to /{uuid}
How do I do that?
I have tried changing to location.assign (SurveyPage/${survey.id})
but it fail because it doesn't find the file without the .html extension
I also tried location.assign (SurveyPage.html/${survey.id}) which also doesn't work.
since you are using NODE for backend - you can build your app using express
https://expressjs.com/en/api.html#req.baseUrl
allows for api like route rewriting to accomplish what you want
can also be done using the more robust Hapi server
https://hapi.dev/
can be done on any language really
slimphp works great for php apps
http://www.slimframework.com/

what's needed in asp file for xmlhttp request to save data to a database

I need to use JavaScript to send data to be stored in a database. Having looked around on-line I think the best way was to use a xmlhttp request to send data to an asp file.
Below is the script I've got to send to 'Receiver.asp'.
Searching the web hasn't helped me uncover the code I need in Receiver.asp.
function postToASP(name, time) {
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var UrlToSend = "Receiver.asp?" +"n=" + name + "t=" + time ;
xmlhttp.open("GET", UrlToSend, false);
xmlhttp.send();
}
Thanks for any help.
Some additional info if needed:
the majority of my code is written in processing
the above js script and the .asp file will be stored in same folder as sketch
eventually i intend to run on a xampp server
First of all, I'm assuming that that's supposed to be client side JavaScript and that it works.
You say you want to use XAMPP. Forget it. Classic ASP is proprietary Microsoft technology which will only run on IIS.
What Receiver.asp has to do is quite simple. First it has to use the request object to retrieve the data it has been sent. (I'm using VBScript as my scripting language because it's what I'm used to, you can use Javascript if you prefer
dim time, name
time = Request.Querystring("t")
name = Request.Querystring("n")
If you were using the post method then you would use Request.Form() Alternatively Request() works for both get and post methods.
Then it's a standard database insert. It doesn't matter if the data is sent in a querystring, in an html form or by an ajax call, the server side code is pretty much the same. Here's a simple tutorial, you should find plenty more if you google
http://www.codefixer.com/tutorials/form_to_database.asp
You haven't said what sort of database you want to write to. You'll need the relevant connection string. Here's a very useful resource with an easy to remember url.
http://www.connectionstrings.com/

Using JavaScript to send String to Servlet, and Results from servlet back to JavaScript

First of all: sorry for my bad grammer. English isn't my native language, but i will try to exlpain my problem as simple as i can.
I'm working on a web-application, where user can enter a link. (Question 1) This link should be send to the server/servlet and will be progressed to other things. (Question 2) After the progression, the servlet will send a json-array (?) back to the javascript-part of my app.
I'm completly new to this kind of stuff, but its very important to me, to find out how this works or better, how i can make this work. Its actually very simple, but i used plenty of weeks and cant figure it out.
The application is using the SAP UI5-libs (Question 3), where i would also like to know, if there is any possible way, to parse JSON with the UI5 libs.
I hope, i could explain my problem good enough, so i can get some help. Thanks to all!
The 'sending' of the string to the server/servlet would happen via ajax in either POST or GET form. That is up to you.
I recommend you use a javascript plugin like jQuery (JQuery Ajax API) because the regular ajax code is a bit messy.
As for the servlet/server communicating back to the client is as simple as writing to the page. In a typical servlet context it would be something like
out.print("This is a message");
where Ajax automatically returns the content of the entire page upon callback.
So in conclusion:
Consider test.jsp your servlet. I wish to send "Hi" from the client (being the browser) via GET to the servlet and I want the servlet to say "Hello" back.
I would open an ajax request of type GET to the url "test.jsp?param=Hi". In the servlet I receive this page request and process it. The servlet discards the parameter because it is not used and outputs "Hello" to the page.
In the client the ajax will have returned "Hello" and I can use this to put it into a var or whatever and all of this happened while not refreshing and not navigating in the original document where I did the javascript.
Another way is using websockets where you basically use sockets in javascript to send and receive any kind of data.
Also please check out this possible duplicate question: How to send a string to a servlet from javascript using xmlhttprequest

How to create a XMLHttpRequest POST within a Polarion Work Item post-save script

I'm currently trying to send data from a Polarion work item to a Java servlet. I have a post-save trigger which upon saving my work activates my javascript.
This script is supposed to check the work item fields for data, validate it and transmit it to my servlet via a POST request.
To enable this post save trigger in Polarion I'm using the following plugin.
https://extensions.polarion.com/extensions/134-fmc-work-item-save
I can already get the data from all the work item fields. The only thing I am unable to do is create a XMLHttpRequest.
var xhr = new XMLHttpRequest();
xhr.open("POST", "/postservlet", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
xhr.send(JSON.stringify({value: "test"}));
returns:
TypeError: this.XMLHttpRequest is not a function
I am by no means an experienced web developer but this means the javascript can't access the browser window global methods since I am using a plugin to load the javascript and it's not being added to the HTML page directly.
Including something like jquery from a CDN is also currently not possible.
I am looking for a way to allow this script to use the XMLHttpRequest or for some other way to enable post requests in Polarion. Either via a different plugin or by editing the source code (if that's even possible).
The XMLHttpRequest-object is not part of the Javascript Context of the JavaSavehook. So you cannot use it.
The Javascript Interpreter"Rhino" does not have an XMLHttpRequest object because it is just wrapping the Java Network classes.
XMLHttpRequest in Rhino? (but I doubt the solution helps you..)

Categories