从javascript函数调用Ajax [英] Ajax call from javascript function

查看:114
本文介绍了从javascript函数调用Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有javascript函数名称函数addOtherTextInDiv()

现在我用来从这个函数调用ajax。我的代码如下:

i have javascript function name function addOtherTextInDiv()
now i use to call ajax from this function.my code is given below:

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;
					}
					
				}
				
		

	}
}





这里ajax不工作。错误的PLZ帮助我。



我尝试过:



从javascript函数调用ajax



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

What I have tried:

call ajax from a javascript function

推荐答案

这里最糟糕的事情是通过浏览器使用任何东西 - 嵌入式ActiveX。对任何事情都是如此,包括Ajax。这根本不是一种合法的方式。它不仅标准,工作,基本上只适用于Microsoft,不是所有系统和浏览器,但也被认为非常不安全,原因很充分。事实上,如果用户试图使用ActiveX,那么关注安全的用户应该将该网站列入黑名单。这将是最严重的安全漏洞之一。



您需要以标准化方式使用Ajax。请参阅:

Ajax

< a href =https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started>入门 - Ajax [ ^ ],

使用XMLHttpRequest - Web API



你也可以找到相当不错的Ajax支持在一些JavaScript库/框架中,特别是jQuery:

Ajax | jQuery API文档

jQuery.ajax()| jQuery API文档



另请参阅Prateek Dalbehera对问题的评论。



Ajax很久以前就已经标准化了。一些非废话浏览器不会不支持它。如果您遇到的浏览器不能以标准方式支持Ajax但需要ActiveX,您可以在代码中果断地检测它并拒绝使用它。它只会使您的软件或网站受益,并且不会吓到真正的用户。



-SA
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


这篇关于从javascript函数调用Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆