JavaScript | get/post请求,处理json
js 发送 get、post请求,post请求发送参数
<script type="text/javascript">
<!--使用XMLHttpRequest-- >
var myUrl = ""
var myHeader_json = {'Content-Type':'application/json; charset=utf-8'} // 处理json文件的请求头
var myHeader = {'Content-Type':'application/x-www-form-urlencoded'}
// get请求:
function request_get() {
var httpRequest = new XMLHttpRequest(); // 第一步:建立所需的对象
httpRequest.open('GET', myUrl, true); // 第二步:打开连接,将请求参数写在url中
httpRequest.send(); // 第三步:发送请求,将请求参数写在URL中
// 获取数据后的处理程序
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var json = httpRequest.responseText; //获取到json字符串,还需解析
console.log(json);
}
};
}
// post请求:
function request_post(){
var httpRequest = new XMLHttpRequest();//第一步:创建需要的对象
httpRequest.open('POST', myUrl, true); //第二步:打开连接
httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");//设置请求头 注:post方式必须设置请求头(在建立连接后设置请求头)
httpRequest.send('firstTest');//发送请求 将情头体写在send中
/**
* 获取数据后的处理程序
*/
httpRequest.onreadystatechange = function () {//请求后的回调接口,可将请求成功后要执行的程序写在其中
if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功
var json = httpRequest.responseText;//获取到服务端返回的数据
console.log(json);
}
};
}
// post方式发送json
function request_postSendJson(){
var httpRequest = new XMLHttpRequest(); //第一步:创建需要的对象
httpRequest.open('POST', myUrl, true); //第二步:打开连接
/***发送json格式文件必须设置请求头 ;如下 - */
httpRequest.setRequestHeader("Content-type","application/json; charset=utf-8"); // 设置请求头,注:post方式必须设置请求头(在建立连接后设置请求头)
var obj = {"busiCode": "deviceInfo","x_token": "","body": {"device_imei": ""}}
httpRequest.send(JSON.stringify(obj));//发送请求 将json写入send中
/**
* 获取数据后的处理程序
*/
httpRequest.onreadystatechange = function () {//请求后的回调接口,可将请求成功后要执行的程序写在其中
if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功
var json = httpRequest.responseText;//获取到服务端返回的数据
console.log(json);
}
};
}
<!--使用XMLHttpRequest-- >
</script>
js接收json并解析
function myLogin(){
var httpRequest = new XMLHttpRequest(); // 第一步:创建需要的对象
httpRequest.open('POST', myUrl, true); // 第二步:打开连接
httpRequest.setRequestHeader("Content-type", "application/json; charset=utf-8"); // 第三步:设置请求头 http://im.bfgps.cn/views/monitor/index.html 要求 HTTP MINE类型 注释:POST方式必须设置请求头
var requestDict = {"busiCode": "memberLogin","body": {"username": "","password": "", "uuid": "", "loginType": "" }} // 第四步:设置要发送的json内容
httpRequest.send(JSON.stringify(requestDict)); // 第五步:发送请求发送请求 将json写入send中
httpRequest.onreadystatechange = function () { // 第六步:处理接收到的数据 请求后的回调接口,可将请求成功后要执行的程序写在其中
if (httpRequest.readyState == 4 && httpRequest.status == 200) { // 第七步:验证请求是否发送成功
var recvText = httpRequest.responseText; // 第八步:获取到服务端返回的数据
var recvJson = JSON.parse(recvText); // 将 json字符串 转化为 json对象
console.log(recvText);
console.log(recvJson['data']['x_token']);
}
};
}
Javascript json字符串与json对象互转
Json字符串 转 Json对象
var json_string = '{"code": 100,"data": {"member_id": 1,"x_token": "","expires_in": 1},"message": "成功"}';
var rtJson = JSON.parse(str); // JSON字符串 2 JSON对象
// 读取方式1
Alert(rtJson.code);
// 读取方式2
Alert(rtJson['code']);
Json对象 转 Json字符串
var json_jsonObj = {"code": 100,"data": {"member_id": 1,"x_token": "","expires_in": 1},"message": "成功"};
var rtText = JSON.stringify(json_jsonObj); // JSON对象 2 JSON字符串
// 读取方式
alert(rtText);