我在javascript中有一个请求代码..我想将其转换为ajax调用..,因为我认为我的代码很旧?你能帮忙吗?
我在js中的功能是:
function loadRest() {
const request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (this.readyState === 4) {
let result = parseResponse(this.status, this.responseText);
if (result != null) {
Rest.rests = result;
createTable();
}
}
};
request.open("GET", Rest.baseURL + "/byCompany/" + logginedCompanyId, true);
request.send();
}
function parseResponse(status, responseText) {
log(responseText);
let responseObject = JSON.parse(responseText);
if (status !== 200 || (responseObject.error && responseObject.error != null)) {
alert("Error: " + responseObject.error);
return null;
}
return responseObject.result;
}
答案 0 :(得分:0)
您可以使用$ .get() 像这样
$。get('Rest.baseURL',function(response){
//
});
答案 1 :(得分:0)
这就是您想要的。
$('#ajax').click(function() {
$.ajax({
type: "GET",
dataType: "json",
url: "localhost:8080/restws/json/product/get",
success: function(data){
let result = JSON.parse(data);
if(result != null) {
Rest.rests = result;
createTable();
}
}
});
});