Click here to Skip to main content
15,664,823 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 code is given below:
var searchID = "";
function GetXmlHttpObject()
	var xmlHttp=null;
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	catch (e)
		// Internet Explorer
		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)
			searchID = strResponseText;
			//document.getElementById("ajaxDivMain").innerHTML = ""

function getData(strText,systemName)
	 //var O = document.getElementById("orderDdl");
	 //orderValue = O.options[O.selectedIndex].value;
	if (xmlHttp==null)
		alert ("Your browser does not support AJAX!");
	//xmlHttp.setRequestHeader("Content-length", url.length); //This line is required for Firefox, only when using POST method
function addOtherTextInDiv()
var stringID = document.getElementById(hiddenIdFName).value;
	if(stringID == "" || stringID == null){
				if (searchID != "0" || searchID != ""){
					stringID = searchID;
					//ID = stringID
					if (systemName == 'exp'  || systemName == 'skill') 
						stringID = "";
						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
Updated 9-Jun-16 7:34am
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    
Prateek Dalbehera 9-Jun-16 5:29am    
a typical jquery ajax call:
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) {
alert('in error');
failure: function (response) {
alert('in failure')

so, I think you need to specify all properties but you missed contentType & dataType in your code...

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:
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.

Share this answer

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