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
Related
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();
}
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 {
}
}
}
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"
I am trying to check if the given url is working or not using ajax. So i modified the Ajax a little bit.
function isServerAlive()
{
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)
{
document.getElementById("myDiv").innerHTML=xmlhttp.status;
}
}
xmlhttp.open("GET","wrongurl",true);
xmlhttp.send();
}
All i want to do is it to display the xmlhttp.status as 404 or 503 when the url is wrong. But it is not printing. Any suggestions?
You have status code available in .status and the text in .statusText
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState == 4)
console.log("Code: " + xmlhttp.status + "Status text: " + xmlhttp.statusText);
}
a respone object is passed to the xmlhttp events, so use this instead:
xmlhttp.onreadystatechange=function(r) {
if (r.readyState == 4) {
document.getElementById("myDiv").innerHTML = r.status;
}
}
I don't think you'd be able to use XHR to check if url is working, as the url has to be from the same origin as the original page. First check if your solution works correctly when accessing a local url.
Hello I want to get xml from Google Weather
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.open("GET", "http://www.google.com/ig/api?weather=london&hl=en", true);
xmlhttp.send(null);
xmlDoc=xmlhttp.responseXML;
It`s not working . Thanks
XMLHttpRequest is asynchronous. You need to use a callback. If you don't want to use a full-fledged library, I recommend using Quirksmode's XHR wrapper:
function callback(xhr)
{
xmlDoc = xhr.responseXML;
// further XML processing here
}
sendRequest('http://www.google.com/ig/api?weather=london&hl=en', callback);
If you absolutely insist on implementing this yourself:
// callback is the same as above
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "http://www.google.com/ig/api?weather=london&hl=en", true);
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState != 4) return;
if (xmlhttp.status != 200 && xmlhttp.status != 304) return;
callback(xmlhttp);
};
xmlhttp.send(null);
Edit
As #remi commented:
I think you'll get a cross domain access exception : you can't make an ajax request to an other domain than your page's. no ?
Which is (for the most part) correct. You'll need to use a server-side proxy, or whatever API that Google provides, instead of a regular XHR.
You can't do this via javascript to to it being a cross-domain request. You'd have to do this server-side.
In PHP you'd use CURL.
What you are trying to do can't be done with Javascript.
Ok here is the code :
<html>
<body>
<script type="text/javascript">
var xmlhttp;
var xmlDoc;
function callback(xhr)
{
xmlDoc = xhr.responseXML;
// further XML processing here
}
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "http://www.google.com/ig/api?weather=london&hl=en", true);
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState != 4) return;
if (xmlhttp.status != 200 && xmlhttp.status != 304) return;
callback(xmlhttp);
};
xmlhttp.send(null);
alert(xmlDoc);
</script>
</body>
</html>
It doesn`t returns any errors but alert returns undefined.