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.
Related
There is a main-feed showing a list of blog post titles. When a title is clicked, I need to display the details of the blog post in a new html file. Below is my current code:
window.location.href = "/viewpost.html";
postID = this.id;
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("view-post-container").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "viewpost.php?q=" + postID, true);
xmlhttp.send();
The problem is, the element view-post-container, is in the file viewpost.html, and so I don't think the PHP file can access it. I would just display the data on the current page (index.php), but I wanted to have individual pages for each post so I can eventually learn how to have dynamic URL's for SEO and sharing purposes. The end goal is having dynamic urls, so maybe there is a better approach? Any help would is much appreciated. Thanks.
just try this, You have to put your code in on window.onload function
window.onload = function() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("view-post-container").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "viewpost.php?q=" + postID, true);
xmlhttp.send();
}
I am checking some date whether available or not from DB using Ajax using below my code,
var url = "ValidateDateFromDB.jsp?cdate="+selecteddate.value;
if (window.XMLHttpRequest)
req = new XMLHttpRequest();
else if (window.ActiveXObject)
req = new ActiveXObject("Microsoft.XMLHTTP");
req.open("GET", url, true);
req.onreadystatechange =callback;
req.send(null);
function callback()
{
if (req.readyState == 4)
{
if (req.status == 200)
{
var message = req.responseText;
alert(message);
if(message.startsWith("Available"))
{
alert("Availabe");
}
else
{
alert("Not Availabe");
}
}
}
}
I am getting output like below image,
It is working fine in firefox but not working in chrome.I checked out that startsWith works only chrome above 40 version.So to fix this i used == operation like below,
if(message=="Available")
{
alert("Availabe");
}
else
{
alert("Not Availabe");
}
But this code always goes to else part that i know why.
someone tell me how to fix this?
In Prestashop I am using alphabitical search module. When I installed the module on my prestashop theme(1.5.0.17) I got an error like
Uncaught TypeError: Cannot call method 'getElementsByTagName' of null
Here is the code for jQuery file
function initXMLHTTPRequest()
{
var xmlHttp = null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function getProdList(id,langid)
{
var xmlHttp=initXMLHTTPRequest();
var str = "value="+id+"&langid="+langid;
var span="";
var url = "modules/alphabetsearch/PHP/searchDetails.php?";
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 )
{
span=span+"<ul style='padding-bottom:20px;'><li><b>"+id+"</b></li>";
var xmldata=xmlHttp.responseXML;
var xmlObj = xmldata.getElementsByTagName("ProductDetails")[0];
var menusize= xmlObj.childNodes[0].childNodes[0].childNodes[0].nodeValue;
var xmlObjlength = xmlObj.childNodes.length;
for(var i=1;i<xmlObjlength;i++)
{
var ProductName=xmlObj.childNodes[i].childNodes[0].childNodes[0].nodeValue;
var productId=xmlObj.childNodes[i].childNodes[1].childNodes[0].nodeValue;
span=span+"<li style='line-height:20px;display:block;height:20px;list-style:none;border-bottom:1px solid #666666;'><a href='product.php?id_product="+productId+"'><div>"+ProductName+"</div></a></li>"
}
if(xmlObjlength==1)
{
document.getElementById("products").innerHTML="";
document.getElementById("products").innerHTML="No Products Under this Alphabet";
}
else{
document.getElementById("products").innerHTML="";
span=span+"</ul>";
document.getElementById("products").innerHTML=span;
}
}
};
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", str.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(str);
}
Can someone kindly tell me how to solve this issue? Any help and suggestions will be really apprecaible. Thanks
The live site can be found here
One of my client had a form developed in component Mosets Tree. In the form when you select a main category it automatically displays the subcategories. Now the issue is; I had to hide some code to stop displaying a few things, after that the java script which was displaying subcategories after we select the main category is not working in IE.
code:
var xmlhttp;
function stateChanged(){
if (xmlhttp.readyState==4) {
document.getElementById("subCatId").innerHTML = xmlhttp.responseText;
}
}
function fnGetSubCategory() {
xmlhttp = GetXmlHttpObject();
var new_cat_id = document.getElementById("new_cat_id").value;
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return true;
}
var url="ps.php?cat_id="+new_cat_id;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function GetXmlHttpObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
It is working fine in all other browsers.
Thanks in advance.
Try creating the object xmlHttp with this code:
function createXmlHttpRequestObject(){
var xmlHttp;
try{
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// If its IE 6 or other version before
var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
// We try all versions
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
try {
//Try creating xmlHttp object
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e){
xmlHttp = false;
}
}
}
// If object doesn't exist sends error
if (!xmlHttp){
alert("Error creating XMLHttpRequest object");
}
else{
return xmlHttp;
}
}
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.