[英]How I can send json in request body with ajax ? just javascript and dont use jQuery
How can I send json in body request with ajax?如何使用 ajax 在正文请求中发送 json? Only use Javascript and do not use jQuery !!!
只使用 Javascript 不使用 jQuery !!! Someone can help I remind you not to use jQuery!!!
有人可以帮我提醒你不要使用 jQuery !!!
You can create function that work like jquery ajax request.您可以创建 function ,其工作方式类似于 jquery ajax 请求。
function AJAX(url, success, async) {
if (window.XMLHttpRequest) {
var request = new XMLHttpRequest;
} else {
// IE6 and IE5 (unnecessary)
var request = new ActiveXObject("Microsoft.XMLHttp");
}
if (async) request.onReadyStateChange = function() {
if (request.readyState == 4) {
success(request.status, request.responseText);
}
};
request.open("GET", url, async);
request.send();
if (!async) success(request.status, request.responseText);
}
I don't got the point.我不明白这一点。 If all you need is to request an URL and send JSON body, you can achieve it simply with
XMLHttpRequest
:如果您只需要请求 URL 并发送 JSON 正文,您可以使用
XMLHttpRequest
简单地实现它:
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) {
console.log('RESPONSE');
}
};
var data = {"email": "hey@mail.com", "password": "101010"}; // JSON YOU SEND
xhr.send(JSON.stringify(data));
Original answer here: Sending a JSON to server and retrieving a JSON in return, without JQuery此处的原始答案: 将 JSON 发送到服务器并检索 JSON 作为回报,没有 JQuery
You can try something like that你可以尝试类似的东西
var xhr = new XMLHttpRequest;
xhr.open('POST', '/your_url');
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
// success
console.log(xhr.responseText);
} else {
// other
console.log('Error, status code = ' + xhr.status + ', response = '+xhr.responseText);
}
};
var payload = JSON.stringify({param1: 1, param2: 'hello'});
xhr.send(payload);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.