node.js - XMLHttpRequest中的url有什么用?
伊谢尔伦
伊谢尔伦 2017-04-17 15:58:38
0
3
497

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文件在这是个什么作用呢?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
大家讲道理

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

洪涛

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

迷茫

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template