JavaScirpt中的XMLHttpRequest对象提供了对 HTTP 协议的完全访问,使用该对象可以在不刷新页面的情况与服务器交互数据。XMLHttpRequest是实现AJAX技术的关键对象,本站曾整理过一篇介绍该对象的文章: JS使用XMLHttpRequest对象与服务器进行数据交互 ,今天将介绍使用XMLHttpRequest对象收发JSON格式数据。
应用场景
在工作中有一个应用需要使用验证码,在用户输入验证码后,使用AJAX技术将用户输入内容提交到服务器端进行验证。服务器端数据的收发都是基于JSON格式的,因此,在发送数据时需要设置数据的请求格式,收到服务器响应内容后也要对数据进行处理。
关键代码
var captcha = document.getElementsByName('captcha')[0];
captcha.onchange = function(){
var xhr = new XMLHttpRequest();
xhr.open("POST", "/captcha", true);
xhr.setRequestHeader('content-type', 'application/json');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if(xhr.getResponseHeader('content-type')==='application/json'){
var result = JSON.parse(xhr.responseText);
if(result.code===-1){
alert('验证码错误');
}
} else {
console.log(xhr.responseText);
}
}
}
var sendData = {captcha:this.value};
xhr.send(JSON.stringify(sendData));
}
相关代码解释
xhr.open(“POST”, “/captcha”, true):这一句传入了三个参数,第一个参数POST是HTTP请求类型为。/captcha是请求路由,即:请求网址。true表示这是一个异步请求。
xhr.setRequestHeader(‘content-type’, ‘application/json’):这一句实际上是在HTPP请求的header中添加content-type。
xhr.getResponseHeader(‘content-type’)===‘application/json’:这一句是判断服务器的响应内容格式,如果是’application/json’格式就说明可以转换为JSON对象,之后客户端就可以按JSON对象格式进行数据处理。
xhr.send(JSON.stringify(sendData)):xhr.send()方法要求传入数据格式是字符串或Document对象,但传入数据是一个Object,所以需要使用JSON.stringify()将其序列化成字符串。
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 7 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 8 天前
所有评论(0)