发送一个JSON到服务器并返回一个JSON,没有JQuery [英] Sending a JSON to server and retrieving a JSON in return, without JQuery
问题描述
我需要向服务器发送一个JSON(我可以将其字符串化),并在用户端检索产生的JSON,而不使用JQuery。
如果我应该使用GET,我该如何传递JSON作为参数?是否有风险太长?
如果我应该使用POST,如何设置 onload的等效值
函数在GET?
或者我应该使用不同的方法?
备注 b 这个问题不是关于发送简单的AJAX。它不应该被重复关闭。 使用POST方法发送和接收JSON格式的数据 HTTP Get请求的长度限制取决于服务器和客户端(浏览器)使用,从2kB - 8kB。如果URI的长度超过了服务器的处理时间,服务器应该返回414(Request-URI Too Long)状态。 注意有人说我可以使用状态名称而不是状态值;换句话说,我可以使用 I need to send a JSON (which I can stringify) to the server and to retrieve the resulting JSON on the user side, without using JQuery. If I should use a GET, how do I pass the JSON as a parameter? Is there a risk it would be too long? If I should use a POST, how do I set the equivalent of an Or should I use a different method? REMARK This question is not about sending a simple AJAX. It should not be closed as duplicate.
The limit of the length of an HTTP Get request is dependent on both the server and the client (browser) used, from 2kB - 8kB. The server should return 414 (Request-URI Too Long) status if an URI is longer than the server can handle. Note Someone said that I could use state names instead of state values; in other words I could use 这篇关于发送一个JSON到服务器并返回一个JSON,没有JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
//使用POST方法以JSON格式发送和接收数据
//
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格式的接收数据
//使用GET方法以JSON格式发送接收数据
//
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格式的数据
<?php
//在服务器端使用PHP
//
header( Content-Type:application / json);
//从使用POST方法发送的JSON构建一个PHP变量
$ v = json_decode(stripslashes(file_get_contents(php:// input)));
//从使用GET方法发送的JSON构建PHP变量$ b $ v = json_decode(stripslashes($ _ GET [data]));
//将PHP变量编码为JSON并在客户端将其发回
echo json_encode($ v);
?>
xhr.readyState === xhr.DONE
而不是 xhr.readyState === 4
问题是Internet Explorer使用不同的状态名称,所以最好使用状态值。onload
function in GET?Sending and receiving data in JSON format using POST method
// 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);
Sending a receiving data in JSON format using GET method
// 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();
Handling data in JSON format on the server-side using PHP
<?php
// 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);
?>
xhr.readyState === xhr.DONE
instead of xhr.readyState === 4
The problem is that Internet Explorer uses different state names so it's better to use state values.