简体   繁体   中英

How is url parameter in open method of XMLHttpRequest used?

MDN tells me that the specification of the XMLHttpRequest open method includes the bstrUrl parameter and that this parameter represents "The requested URL." Vague to say the least.

www.help.dottoro.com tells me that the parameter contains the "String that specifies the URL where the request needs to be sent."

W3Schools has this example:

<!DOCTYPE html>
<html>
<body>

<div id="demo"><h2>Let AJAX change this text</h2></div>

<button type="button" onclick="loadDoc()">Change Content</button>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo").innerHTML = xhttp.responseText;
    }
  };
  xhttp.open("GET", "ajax_info.txt", true);
  xhttp.send();
}
</script>

</body>
</html>  

This example triggers the text to be displayed when the button is clicked.

My book tells me it is "The path to the page that will handle the request." Then I see an example in my book of where a .json file is specified in that parameter which contains data that is then displayed in html.

I'm confused. How does js know what the specified file is for?

How is url parameter in open method of XMLHttpRequest used?

It's the URL that the XMLHttpRequest object will ask the browser to send the GET or POST to.

How does js know what the specified file is for?

The person writing the JavaScript writes code that knows what to do with the specified resource.

In your w3schools example, the code knows that it's requesting something that will return HTML it wants to display in the demo element.

If the request were for JSON, the code would handle a successful request by parsing the JSON and doing something with the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM