Am trying to get http response from php web service in javascript, but getting null in firefox and chrome. plz tell me where am doing mistake here is my code,
function fetch_details()
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest()
alert("first");
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP")
alert("sec");
}
xhttp.open("GET","url.com",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
alert(xmlDoc.getElementsByTagName("Inbox")[0].childNodes[0].nodeValue);
}
I have tried with ajax also but am not getting http response here is my code, please guide me
var xmlhttp = null;
var url = "url.com";
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
alert(xmlhttp); //make sure that Browser supports overrideMimeType
if ( typeof xmlhttp.overrideMimeType != 'undefined')
{
xmlhttp.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert('Perhaps your browser does not support xmlhttprequests?');
}
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4)
{
alert(xmlhttp.responseXML);
}
};
}
// Make the actual request
xmlhttp.send(null);
I am getting xmlhttp.readyState = 4 xmlhttp.status = 0 xmlhttp.responseText = ""
plz tell me where am doing mistake
Your doing a cross domain request.
You are only allowed to do xmlhttp requests to the same host.
I can't read any of that but Chrome has a JavaScript console that will probably tell you what you're doing wrong.
It's a cross-domain issue, to resolve it the server response header should contain "access-control-allow-origin"
If your server is coded in PHP, the header should be like the following example:
<?php
header('Content-type: text/html');
header('Access-Control-Allow-Origin: *');
$uri = 'http'. ($_SERVER['HTTPS'] ? 's' : null) .'://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo('<p>This information has come from ' . $uri . '</p>');
?>
Related
I am new to ajax. I wanted to create a simple webpage where it contains a button if clicked returns image dynamically.But the responseXML returns null value. Here is part of javascript code:
function process()
{
if(xmlhttp.readyState==4 || xmlhttp.readyState==0)
{
xmlhttp.open("GET","image.php",true);
xmlhttp.onreadystatechange = handleserverresponse;
xmlhttp.send();
}else{
setTimeout('process()',1000);
}
}
function handleserverresponse()
{
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
xmlResponse = xmlhttp.responseXML;
imag = xmlResponse.documentElement.firstChild.data;
document.getElementById("divimg").innerHTML=imag;
}
else{
alert("something went wrong");
}
}
here is php code:
<?php
header('Content-Type:text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo "<res>";
echo "<img src="a.jpg"/>";
echo "</res>";
?>
Your HTTP request is asynchronous. xmlhttp.responseXML won't have some value until xmlhttp.readyState has the value of 4.
var url = "http://localhost/xml.php?type=xml";
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp) {
xmlhttp.open("GET", url, true);
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
alert(xmlhttp.responseXML);
}
};
xmlhttp.send();
}
Additionaly, I don't think you need the setRequestHeader line. XML MIME type is required for response, not for request. Also, please respect good coding practices (don't forget var, DRY, etc.)
The reason responseXML was null because there is a mistake in PHP file.
I guess We cannot send HTML tags when content type is xml. Instead what we can do is echo source of image and modify JavaScript file to take that source and display using img tag.
function getXmlHttpRequestObject()
{
var xmlHttp = false;
if (window.XMLHttpRequest)
{
return new XMLHttpRequest(); //To support the browsers IE7+, Firefox, Chrome, Opera, Safari
}
else if(window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP"); // For the browsers IE6, IE5
}
else
{
alert("Error due to old verion of browser upgrade your browser");
}
}
var xmlhttp = new getXmlHttpRequestObject(); //xmlhttp holds the ajax object
function servletPost()
{
if(xmlhttp)
{
var comp_to = document.getElementById("comp_to").value;
var comp_subject = document.getElementById("comp_subject").value;
var comp_letter = document.getElementById("comp_letter").value;
var date_time = document.getElementById("date_time").value;
if(comp_to==""||comp_subject==""||comp_letter==""||date_time=="")
{
document.getElementById("redSignal").style.display='block';
document.getElementById("redSignal").innerHTML="All Fields are necessary";
}
else
{
xmlhttp.open("POST","complaintHandler",true);
xmlhttp.onreadystatechange = handleServletPost;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var data_string="to="+comp_to+"&subject="+comp_subject+"&complaint="+comp_letter+"&date_time="+date_time;
xmlhttp.send(data_string);
}
}
}
function handleServletPost()
{
if (xmlhttp.readyState == 4)
{
if(xmlhttp.status == 200)
{
document.getElementById("greenSignal").style.display='block';
document.getElementById("greenSignal").innerHTML=xmlhttp.responseText;
}
else
{
document.getElementById("redSignal").style.display='block';
document.getElementById("redSignal").innerHTML="Error Code ="+xmlhttp.status;
}
}
}
I am getting the problem of error code 404
What could be the problem in this code? Please help me.
Error 404 itself says that your URL is wrong .
xmlhttp.open("POST","complaintHandler-wrong",true);
check this URL 1st .
ERROR 404 Says. The requested Http request is not present or wrong.
Please check your "complaintHandler" It might be complaintHandler.jsp , kind of...
Please go through this tutorial for your future use.
Following code is working fine with IE8 but is not working with google chrome. What i need to add/modify in the code to make it work with chrome also. I have heard of http://en.wikipedia.org/wiki/Same_origin_policy and http://en.wikipedia.org/wiki/Cross-origin_resource_sharing.
function clickMe()
{
var xmlHttp = null;
var Url = "http://www.w3schools.com/ajax/gethint.asp?q=u";
//var Url = "file:///D:/Durgesh/test.html";
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
if ( xmlHttp.readyState == 4 )
{
alert("status Code: " + xmlHttp.status);
if ( xmlHttp.responseText == "Not found" )
{
alert("Not Found");
}
else
{
alert(xmlHttp.responseText);
}
}
}
xmlHttp.open( "GET", Url, true );
xmlHttp.setRequestHeader('Access-Control-Allow-Origin', '*');
xmlHttp.send( null );
}
Access-Control-Allow-Origin must be server side. ;)
I have a problem in which I am calling a url through ajax, but I am having some problem in handling the response.
The url is returning the response, when I directly call it from the browser, but when I am using it in my ajax call I am having some problem in handling it.
I have used both the property(responseText and responseXML) of the XMLHTTPREQUEST object.
my code is::
function postRequest(strURL)
{
var xmlHttp;
if (window.XMLHttpRequest) // Mozilla, Safari, ...
{
var xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) // IE
{
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open('GET', strURL, true);
xmlHttp.setRequestHeader('Content-Type', 'text/html; charset=ISO-8859-1');
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
alert("Status =4");
alert(xmlHttp.responseXML);
alert(xmlHttp.responseText);
}
}
xmlHttp.send(strURL);
}
The url is:: http://www.amazon.com/gp/aag/ajax/paginatedFeedback.html?seller=A3QGTRL0G4B98R&isAmazonFulfilled=&isCBA=&marketplaceID=ATVPDKIKX0DER&asin=&ref_=aag_m_fb&¤tPage=1
Please suggest anything.
I think that you can't make calls to another domain. You can read more about AJAX Cross-Domain in G
We are running windows IIS 6 and use it's native ability to protect files with Windows Authentication as our login method.
It works fine, except that when I try to post method XMLHttpRequest from IE i get the login dialog again, which causes the request to fail.
The weird thing is that Mozilla and Safari work well.
Is there something I can do with the headers or something to make IIS recognize it as the same session, and not promt a re-login?
function ajaxQuery(method, url, params, asynchronous, readyFunction, is_done) {
if (asynchronous == null) {
asynchronous = true;
}
//alert("URL: "+url);
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) {
readyFunction(xmlhttp.responseText);
if (is_done) {
is_done("ok");
}
}
}
if (method.toLowerCase() == "get") {
url += "?" + params;
params = null;
}
debug = url;
xmlhttp.open(method, url, asynchronous);
if (method.toLowerCase() == "post") {
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
debug = params.length;
}
xmlhttp.send(params);
}
Check the response headers coming back. We had the same problem with an app that was (intentionally) returning a 401 code. IIS caught that code and added the WWW-Authenticate header which forces IE to display the login. We were able to fix it by returning 403 instead.