Calling Servlet using Ajax call in Internet Explorer - javascript

I am calling Servlet through Ajax Call if I Run this code in Mozilla FireFox its working fine but If I run my code in Internet Explorer 8 its not working.Please Could any one help me.thanks.
My code:
function getXMLObject() //XML OBJECT
{
var xmlHttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}
var xmlhttp = new getXMLObject();
function HomeWorkajaxFunction(param)
{
if (xmlhttp) {
var param1 = document.getElementById("selectError3").value;
xmlhttp.open("GET", "SubjectServlet?sec=" + param + "&gdid=" + param1, true); //gettime will be the servlet name
xmlhttp.onreadystatechange = handleServerResponse1;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(null);
}
}
function handleServerResponse1() {
// alert("11");
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
document.getElementById("subject").innerHTML = "";
document.getElementById("subject").innerHTML = xmlhttp.responseText;
}
else {
}
}
}

Related

save url responce into variable with javascript / html

in a webpage i would like to collect a response from another web server at a given URL address.
let's say someone else has a server at http://mysite/123 that responds with a simple string. (without headers and stuff).
what is the most SIMPLE way to get javascript on my webpage to collect a url's raw response in preferably a byte array variable? though i would except an answer that saves in string to get me going. this is an exact copy paste from my html document and its not working for me.
thanks!
<script>
var txt = "";
txt=httpGet("https://www.google.com");
alert(txt.length.toString());
function httpGet(theUrl) {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false);
xmlhttp.send();
}
</script>
So I'd have to say your best bet would be to look into making an HTTP (or XHR) request from javascript.
check: Return HTML content as a string, given URL. Javascript Function
function httpGet(theUrl)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false );
xmlhttp.send();
}

Error incorrect function in XMLHttpRequest.open() method

I done a ajax call to local http server but I got error in xmlhttp.open("GET", "http://localhost//push", true); incorrect function in IE 11, Below is my full code:
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
....some code.....
}}
}
xmlhttp.open("GET", "http://localhost//push", true);
xmlhttp.send();
}
IE uses new XMLHttpRequest();
Finally i got the answer very silly issue, In chrome URL can be "http://localhost//push" but in IE it should have backslash instead of forward slash "http:\\localhost\\push" or else it will show incorrect function

XMLHttpRequest responseXML is always null

I am calling a asmx web service like this
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
var data = xmlhttp.responseText;
var xmlDoc = xmlhttp.responseXML;
}
}
xmlhttp.open("GET", "https://Service/ServiceName.asmx/method?query=data1&count=1",true);
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send();
even after the readystate being 4, I get responseXML as null and responseText as empty. whereas the url
"https://Service/ServiceName.asmx/method?query=data1&count=1"
works perfectly in the browser.
Please help.
Use a relative path:
with(new XMLHttpRequest)
{
open("GET","/Service/ServiceName.asmx/method?query=data1&count=1",true);
setRequestHeader("Foo", "Bar");
send("");
onreadystatechange = handler;
}
function handler(event)
{
!!event.target && !!event.target.readyState && event.target.readyState === 4 && ( console.log(event) );
}
If that doesn't work, try loading the URL from JavaScript to check for routing issues:
window.location = "/Service/ServiceName.asmx/method?query=data1&count=1"

Javascript function in header showing as undefined

<script type="text/javascript">
function centerItem(id,size)
{
var pad = (window.innerWidth - size)/2;
document.getElementById(id).style.marginLeft = pad+"px";
document.getElementById(id).style.marginRight = pad+"px";
}
function login()
{
document.getElementById('box').innerHTML="<img src="img/pleasewait.gif" />";
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('box').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","http://[lan ip]/Athena/lib/ajax/login.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("email="+email+"&pass="+pass);
}
</script>
That is in my <head> section, and I'm calling it using this.
<script type="text/javascript">centerItem('login',210);</script>
However, I'm getting an error saying "Uncaught ReferenceError: centerItem is not defined
(anonymous function)"
Any thoughts?
document.getElementById('box').innerHTML="<img src="img/pleasewait.gif" />";
Should really be:
document.getElementById('box').innerHTML="<img src=\"img/pleasewait.gif\" />"
you need to escape the double quotes when creating the image tag.
And you should cache your selected elements. The result would be something like:
function centerItem(id, size) {
var pad = (window.innerWidth - size)/2,
elem = document.getElementById(id);
elem.style.marginLeft = pad+"px";
elem.style.marginRight = pad+"px";
}
function login() {
var xmlhttp;
var box = document.getElementById('box');
box.innerHTML="<img src=\"img/pleasewait.gif\" />";
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
box.innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("POST","http://[lan ip]/Athena/lib/ajax/login.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("email="+email+"&pass="+pass);
}

Ajax code doesn't works in IE

<script type="text/javascript">
function showState(str){
if (str.length==0){
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("state").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getState.php?cid="+str,true);
xmlhttp.send();
}
</script>
This code doesn't works in IE but fine in mozilla and chrome
You have to call send(null) on the xmlhttp-Object.
Just add
xmlhttp.send(null);
This will actually send the request.
Have you tried the following:
function createXMLHttpRequest(){
var xmlHttp = null;
if(typeof XMLHttpRequest != "undefined"){
xmlHttp = new XMLHttpRequest();
}
else if(typeof window.ActiveXObject != "undefined"){
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
}
catch(e){
try {
xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
}
catch(e){
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
xmlHttp = null;
}
}
}
}
return xmlHttp;
}
source (http://robertnyman.com/2007/04/04/weird-xmlhttprequest-error-in-ie-just-one-call-allowed/)

Categories

Resources