jquery修改服务器json,在没有JQuery的情况下将JSON发送到服务器并获取JSON作为回报...

小编典典

使用POST方法以JSON格式发送和接收数据

// Sending and receiving data in JSON format using POST method

//

var xhr = new XMLHttpRequest();

var url = "url";

xhr.open("POST", url, true);

xhr.setRequestHeader("Content-Type", "application/json");

xhr.onreadystatechange = function () {

if (xhr.readyState === 4 && xhr.status === 200) {

var json = JSON.parse(xhr.responseText);

console.log(json.email + ", " + json.password);

}

};

var data = JSON.stringify({"email": "hey@mail.com", "password": "101010"});

xhr.send(data);

使用GET方法以JSON格式发送接收数据

// Sending a receiving data in JSON format using GET method

//

var xhr = new XMLHttpRequest();

var url = "url?data=" + encodeURIComponent(JSON.stringify({"email": "hey@mail.com", "password": "101010"}));

xhr.open("GET", url, true);

xhr.setRequestHeader("Content-Type", "application/json");

xhr.onreadystatechange = function () {

if (xhr.readyState === 4 && xhr.status === 200) {

var json = JSON.parse(xhr.responseText);

console.log(json.email + ", " + json.password);

}

};

xhr.send();

使用PHP在服务器端处理JSON格式的数据

// Handling data in JSON format on the server-side using PHP

//

header("Content-Type: application/json");

// build a PHP variable from JSON sent using POST method

$v = json_decode(stripslashes(file_get_contents("php://input")));

// build a PHP variable from JSON sent using GET method

$v = json_decode(stripslashes($_GET["data"]));

// encode the PHP variable to JSON and send it back on client-side

echo json_encode($v);

?>

HTTP

Get请求的长度限制取决于所使用的服务器和客户端(浏览器),范围从2kB-8kB。如果URI的长度超过服务器的处理能力,则服务器应返回414(请求URI太长)状态。

注意 有人说我可以使用状态名称代替状态值。换句话说,我可以使用xhr.readyState ===

xhr.DONE代替。xhr.readyState === 4问题是Internet Explorer使用不同的状态名称,因此最好使用状态值。

2020-05-01

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值