XMLHttpRequest
方法也用于将数据发布到服务器。这是一个例子: req = new XMLHttpRequest();你以前见过其中几种方法。这里
req.open( “POST”,网址,真实);
req.setRequestHeader( '内容 - 类型', '文本/纯');
req.onreadystatechange =函数(){
if(req.readyState == 4 && req.status == 200){
document.getElementsByClassName( '信息')[0] = .innerHTML req.responseText;
}
};
req.send(用户名);
open
方法将请求初始化为对外部资源的给定URL的“POST”,并使用true
布尔值使其异步。 setRequestHeader
方法设置HTTP请求标头的值,该标头包含有关发件人和请求的信息。它必须在open
方法之后调用,但在send
方法之前调用。这两个参数是标题的名称和要设置为该标题正文的值。接下来, onreadystatechange
事件侦听器处理请求状态的更改。 readyState
为4表示操作已完成, status
为200表示该操作成功。文档的HTML可以更新。最后, send
方法使用userName
值发送请求,该值由用户在input
字段中给出。 XMLHttpRequest
。
testString: 'assert(code.match(/new\s+?XMLHttpRequest\(\s*?\)/g), "Your code should create a new XMLHttpRequest
.");'
- text: 您的代码应使用open
方法初始化对服务器的“POST”请求。
testString: 'assert(code.match(/\.open\(\s*?("|")POST\1\s*?,\s*?url\s*?,\s*?true\s*?\)/g), "Your code should use the open
method to initialize a "POST" request to the server.");'
- text: 您的代码应使用setRequestHeader
方法。
testString: 'assert(code.match(/\.setRequestHeader\(\s*?("|")Content-Type\1\s*?,\s*?("|")text\/plain\2\s*?\)/g), "Your code should use the setRequestHeader
method.");'
- text: 您的代码应该将onreadystatechange
事件处理程序设置为函数。
testString: 'assert(code.match(/\.onreadystatechange\s*?=/g), "Your code should have an onreadystatechange
event handler set to a function.");'
- text: 您的代码应该获取带有类message
的元素,并将其内部HTML更改为responseText
。
testString: 'assert(code.match(/document\.getElementsByClassName\(\s*?("|")message\1\s*?\)\[0\]\.innerHTML\s*?=\s*?.+?\.responseText/g), "Your code should get the element with class message
and change its inner HTML to the responseText
.");'
- text: 您的代码应使用send
方法。
testString: 'assert(code.match(/\.send\(\s*?userName\s*?\)/g), "Your code should use the send
method.");'
```
```