Implement HTTP post in Javascript - javascript

Hello
I am doing an analytical project for delay tolerance for various applications. So I need to build a portable application to take user feedback. I am trying to do this using Javascript. Whenever, the user clicks a button on the JS page I have to send a request to the HTTP server setup on the router to change the delay. I want to implement this as a HTTP request (POST). I know Javascript. I found ways like Jquery, Protocol but I intend to send only one parameter(need not be hidden) Can some one help me with this?

Your asking how to send a ajax post w/ a url param?
xhr=new XMLHttpRequest();
xhr.open("POST", "http://helloasdf.cloudfoundry.com/post.token?key=asdf", true);
xhr.send(null);

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.

Call a rest webservice using 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.

Javascript, saving user details to a database

I'm teaching myself some web development on the side, and just for fun I'm trying to build a small e-commerce website for my gf (She might actually use it for a business idea if I don't get side tracked and not finish in time)
So I've managed to build a basic front end with html, css and javascript... The 1st hurdle I've run into is saving basic user details to a database, could someone please guide me in the right direction on what would be the quickest and easiest way to accomplish this.
The only other programming language I have a decent grasp of (certification level) is Java
Thanks in advance
Nowadays, the best way to store user details is probably to NOT store them at all lol. If you can use any third party authentication like Facebook or google, i really think you should give it a try. If you really feel like doing your own login/sign up. It's gonna be in the back-end. Using PHP is easy if you don't have any experience in that kind of stuff.
This is an example of something not too bad
hope it answers your question. If not, let me know and i can give you some more details !
This question can not be answered in a few sentences. But have a look:
Javascript is executed on the client side while PHP for example is executed on the server. You need to send a so called HTTP Request to your PHP server using javascript. This can be done with jQuery via ajax() function or using native JS like:
var xmlhttp = new XMLHttpRequest();
var url = "myTutorials.txt";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
console.log(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
http://www.w3schools.com/json/json_http.asp
but i strongly recommend u to use jQuery:
http://api.jquery.com/jquery.ajax/
What happens during such a http request? The Client (your PC) sends data via an URL to the webserver and u receive it in the response. (have a look at the response in your development tool)
greetings

POST without redirecting page

Hi
I am writing an application where I want to post data after clicking send button, it will post data to some web-action and gets processed, after processing it will be redirected to some other jsp page but I want to be in the same page from where I click send button. I am not sure of XMLHttpRequest method.
Any ideas to get this done highly appreciated.
If you wanna do using Java script and Ajax (As you have in your question tags) then Try following:
function fun() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "Serv?req_id=1";
xmlhttp.open("POST", url, false);
xmlhttp.send(null);
var respo= xmlhttp.responseText;
document.getElementById("some_id").innerHTML = xmlhttp.responseText;
}
Call fun() on onclick event of send button.
Hope this helps.
You can use jQuery.ajax() - it's a convenient wrapper for XMLHttpRequest.
You can use jQuery. It will save some time. It is cross browser compatible (i.e. hides the cross browser compatibility techniques from you).
http://api.jquery.com/jQuery.post/
http://jquery.com/
You can issue a 302 request back to the page you came from as the response from the POST request. If you don't want to POST at all I can show you how to do this through AJAX.
The reason you want to use the GET->POST->GET is so that if the user hits the back button it doesn't re-post on you.
Here's a great beginner's tutorial on what you're looking to do (with jQuery):
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
A 302 request back approach seems to be good for this, If you were using ajax or XMLHttpRequest object, it wouldn't be that problem, but here you can set redirection header to redirect back on the same script that process your query-string.
However you cannot do post without a redirection.
Passing values from one jsp page to another jsp page
I know this is a bit late but for the sake of sharing knowledge, I found a good article here: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
On it, is a very thorough example of how to do what you are asking for.
This is done, as mentioned above, with AJAX/jQuery, this tutorial makes use of that.

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