学习代码,摘抄
function galasysJsonGet(url, json, callback, isAsync) {
var isAsync = isAsync == undefined ? false : isAsync;
if (url == '' || url == null || url == undefined) {
return false;
}
else {
if (typeof (json) == 'object') {
json = JSON.stringify(json);
}
var ajaxRequest = $.ajax({
url: url,
method: 'get',
datatype: 'json',
data: json,
contentType: 'application/json;charset=utf-8',
async: isAsync,
cache: false
});
ajaxRequest.done(function (msg) {
if (callback != null) {
callback(msg);
}
});
ajaxRequest.fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}
}
function galasysJsonPut(url, json, callback, isAsync) {
//默认异步
var isAsync = isAsync == undefined ? true : isAsync;
if (url == '' || url == null || url == undefined) {
return false;
}
else {
if (typeof (json) == 'object') {
json = JSON.stringify(json);
}
//ajax请求
var ajaxRequest = $.ajax({
url: url,
method: 'put',
datatype: 'json',
data: json,
contentType: 'application/json;charset=utf-8',
async: isAsync,
cache: false
});
ajaxRequest.done(function (msg) {
if (callback != null) {
callback(msg);
}
});
ajaxRequest.fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}
}
function galasysJsonPost(url, json, callback, isAsync) {
//默认异步
var isAsync = isAsync == undefined ? false : isAsync;
if (url == '' || url == null || url == undefined) {
return false;
}
else {
if (typeof (json) == 'object') {
json = JSON.stringify(json);
}
//ajax请求
var ajaxRequest = $.ajax({
url: url,
method: 'post',
datatype: 'json',
data: json,
contentType: 'application/json;charset=utf-8',
async: isAsync,
cache: false
});
ajaxRequest.done(function (msg) {
if (callback != null) {
callback(msg);
}
});
ajaxRequest.fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}
}

本文介绍了使用 AJAX 封装 GET、PUT 和 POST 方法处理 JSON 数据的三个 JavaScript 函数。这些函数简化了与服务器进行 JSON 格式的交互过程,并提供了错误处理机制。
557

被折叠的 条评论
为什么被折叠?



