Click here to Skip to main content
15,037,178 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have javascript function name function addOtherTextInDiv()
now i use to call ajax from this function.my code is given below:
JavaScript
var searchID = "";
function GetXmlHttpObject()
{
	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 stateChanged() 
{ 
	var strResponseText;
	
	if (xmlHttp.readyState==4)
	{ 
		if(xmlHttp.status == 200)
		{
			strResponseText=xmlHttp.responseText;
			searchID = strResponseText;
			//document.getElementById("ajaxDivMain").innerHTML = ""
			//document.getElementById("ajaxDivMain").innerHTML=strResponseText;
		}
	}
}

function getData(strText,systemName)
{
	xmlHttp=GetXmlHttpObject()
	
	
	 
	 //var O = document.getElementById("orderDdl");
	 //orderValue = O.options[O.selectedIndex].value;
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		//window.location.href=gURL;
		return;
	}
	url="getSearchStringID.asp?data="+strText+"&system="+systemName;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	//xmlHttp.setRequestHeader("Content-length", url.length); //This line is required for Firefox, only when using POST method
	xmlHttp.send(url);
	
}
function addOtherTextInDiv()
{
var stringID = document.getElementById(hiddenIdFName).value;
	
	if(stringID == "" || stringID == null){
		
			
			getData(stringValue,systemName)
				if (searchID != "0" || searchID != ""){
					stringID = searchID;
					//ID = stringID
				}else{
					if (systemName == 'exp'  || systemName == 'skill') 
					{
						stringID = "";
					}else{
						alert("Not a valid data!");
						return false;
					}
					
				}
				
		

	}
}


here ajax is not working.whats the wrong plz help me.

What I have tried:

call ajax from a javascript function
Posted
Updated 9-Jun-16 7:34am
Comments
Sinisa Hajnal 9-Jun-16 3:41am
   
"not working" is not helpful. What error do you get (if any)? Which line fails? You're not handling any status except 200, put am else block and see what do you get.
Member 9361273 9-Jun-16 3:48am
   
its readyState is always get 1 thats why stateChanged() can not work properly.So why it show 1
Sinisa Hajnal 9-Jun-16 3:56am
   
It would appear that you never reach RequestReceived status...did you try putting full URL instead of just page name?
Member 9361273 9-Jun-16 4:29am
   
yes.
Prateek Dalbehera 9-Jun-16 5:29am
   
a typical jquery ajax call:
$.ajax({
type: "POST",
url: "/Home/GetCities",
data: '{text:"' + request.term + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
},
error: function (response, status, error) {
debugger
alert('in error');
alert(response.responseText);
},
failure: function (response) {
alert('in failure')
alert(response.responseText);
}
});

so, I think you need to specify all properties but you missed contentType & dataType in your code...
   
Please disregard my previous comment — you correctly pointed out the use of jQuery here.
It's just not the only (legitimate) way of using Ajax — please see also Solution 1.
I credited your comment.
—SA

1 solution

The nastiest thing here is to use anything via the browser-embedded ActiveX. It's true for anything at all, including Ajax. This is not a legitimate way at all. Not only it is not standard, working, basically, only for Microsoft, not all systems and browsers, but is also considered very unsafe, by quite good reasons. In fact, the users concerned with safety should blacklist the site if the spot that it tries to use ActiveX. It would be one of the worst safety breach.

You need to use Ajax in a standardized way. Please see:
Ajax,
Getting Started — Ajax[^],
Using XMLHttpRequest — Web APIs.

You can also find quite good Ajax support in some JavaScript libraries/frameworks, notably jQuery:
Ajax | jQuery API Documentation,
jQuery.ajax() | jQuery API Documentation.

See also the comment to the question by Prateek Dalbehera.

Ajax has been standardized a long time ago. It's hardly unlikely that some non-nonsense browser would fail to support it. If you ever face a browser which does not support Ajax in a standard way but requires ActiveX, you can decisively detect it in your code and deny working with it. It will only benefit your software or site and won't scare of real users.

—SA
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900