开发者

AJAX function(this)

开发者 https://www.devze.com 2023-02-28 16:12 出处:网络
I am not familiar with ajax, I am in the process of learning, but as far as I know, it utilizes javascript to access the DOM, so my question is, is it possible to put an argument inside a function?

I am not familiar with ajax, I am in the process of learning, but as far as I know, it utilizes javascript to access the DOM, so my question is, is it possible to put an argument inside a function?

<script type="text/javascript">
function loadXMLDoc( * * this * * ) {
    var xmlhttp;
    i开发者_开发百科f (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", ""
    test.php ? access = "**+this**", false);
    xmlhttp.send();
    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
</script>

Shouldn't this work?

Thank you so much for your help.


this in JS changes according to the context you are running in. Depending on how you call it, it will change.

Read this article for more information : http://www.quirksmode.org/js/this.html

xmlhttp.open("GET",""test.php?access="**+this**",false);

This call wouldn't make much sense since this refers to an object, but what you're actually attempting to do is string concatenation (adding two strings together).

If you need to make variable calls, use a variable instead of the this keyword.

function loadXMLDoc(accessVar) {
    ....
    xmlhttp.open("GET","test.php?access=" +accessVar ,false);
}

loadXMLDoc('accessIdentifier'); //passes the value 'accessIdentifier' to your method so it is passed along in the querystring. 


You're close. It looks like you're using the example from w3schools. Be warned, that site is not always the most reliable source. See http://w3fools.com/ for more info.

Look over their code again, as it is taken from their page:

var xmlhttp;
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("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

As you can see, the "on success" code is located within the onreadystatechange event handler. Your example code has three problems: one with the way you're passing a variable, two with the way you use this, and three that your response handler won't work as expected.

var xmlhttp;
var var1 = 'testdata';
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)
    handleAjaxResponse(responseText);
  }
xmlhttp.open("GET","ajax_info.php?var1="+var1,true);
xmlhttp.send();

function handleAjaxResponse(resp) {
  document.getElementById("myDiv").innerHTML=resp;
}

In terms of this, it refers to the current scope of execution, a different subject entirely. It isn't a variable you'd pass via AJAX.

0

精彩评论

暂无评论...
验证码 换一张
取 消