search

Home  >  Q&A  >  body text

node.js - XMLHttpRequest中的url有什么用?

1.前端请求:

 var obtn = document.getElementById('btn');//按钮button
 var oh = document.getElementById('h');//标题h
 var xmlhttp = new XMLHttpRequest();
obtn.onclick = function() {
        xmlhttp.open("get", "xxxx", true);
        xmlhttp.send();
    };
xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            oh.innerHTML = xmlhttp.responseText;
        }
    }

2.node.js原生

var http = require('http');
  .....
http.createServer(function(req, res) {
    .......
    if (url.pathname == 'xxxx') {
        res.end("收到请求");
    }
}).listen(8000, function() {
    console.log('server on 8000 port');
})

点击button后发送请求,后台判断url做出相应的操作(在这里是将 h1的innerHTML更改为“收到请求”)。

我并没有使用到xxxx文件,所以请问这个xxxx文件在这是个什么作用呢?

伊谢尔伦伊谢尔伦2787 days ago490

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 16:00:38

    url.pathname is the requested resource path, it has nothing to do with whether the resource is available on your server

    首先一个url
    http://www.baidu.com:8000/hello?q=sss
    
    可拆分成好些组成部分, 如:
    protocol    http://
    host        www.baidu.com
    port        8000
    pathname    /hello
    query       ?q=sss
    
    等等  

    It just so happens that you are visiting http://localhost:8000/xxxx

    Indicate that the pathname of your request is /xxxx. What content you want to return is up to your code settings

    Don’t limit your thinking to whether there are file resources on the server. This can be agreed upon as a secret code to request Tudou to return Xihongxiao

    You still have a long way to go with the concept of wsgi, I suggest you go directly to the express tutorial

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:00:38

    The xxxx you have here is just the path, it is not the path of a file or a URL.

    reply
    0
  • 迷茫

    迷茫2017-04-17 16:00:38

    xxx is the address to which you want to send this request.
    Send it to the port 8000 where your node is started

    reply
    0
  • Cancelreply