您可以覆盖
.getJSON
用你自己的方法。
土著人
getJSON
方法如下:
https://j11y.io/jquery/#v=git&fn=jQuery.getJSON
function (url, data, callback) {
return jQuery.get(url, data, callback, "json");
}
这是一件很简单的事情,可以接受传递给
盖特森
通过你的
拥有
回调到
jQuery.get
具有
'text'
而不是
'json'
,使用自定义的reviver解析JSON,然后调用原始回调。
一个复杂的问题是,第二个参数传递给
盖特森
是可选的(与请求一起发送的数据)。
假设始终使用success函数(第三个和最后一个参数),则可以使用rest参数和
pop()
以获得传递的成功函数。创建一个自定义成功函数,该函数接受文本响应并使用自定义
JSON.parse
,然后用创建的对象调用传递的success函数。
const dequoteDigits = json => JSON.parse(
json,
(key, val) => (
typeof val === 'string' && /^[+-]?[0-9]+(?:\.[0-9]+)$/.test(val)
? Number(val)
: val
)
);
jQuery.getJSON = function (url, ...rest) {
// normal parameters: url, data (optional), success
const success = rest.pop();
const customSuccess = function(textResponse, status, jqXHR) {
const obj = dequoteDigits(textResponse);
success.call(this, obj, status, jqXHR);
};
// spread the possibly-empty empty array "rest" to put "data" into argument list
return jQuery.get(url, ...rest, customSuccess, 'text');
};
jQuery.getJSON('https://jsonplaceholder.typicode.com/users', (obj) => {
console.log(obj);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
上面的代码段输出与请求的URL之间的差异
https://jsonplaceholder.typicode.com/users
是纬度和经度属性值已转换为数字,而不是剩余的字符串。
如果你曾经使用过
await
如果没有成功的回调,则需要覆盖
then
返回的promise的属性,尽管处理可选的success回调会使代码的逻辑更加糟糕:
const dequoteDigits = json => JSON.parse(
json,
(key, val) => (
typeof val === 'string' && /^[+-]?[0-9]+(?:\.[0-9]+)$/.test(val)
? Number(val)
: val
)
);
jQuery.getJSON = function (url, ...rest) {
// normal parameters: url, data (optional), success (optional)
const data = rest.length && typeof rest[0] !== 'function'
? [rest.shift()]
: [];
const newSuccessArr = typeof rest[0] === 'function'
? [function(textResponse, status, jqXHR) {
const obj = dequoteDigits(textResponse);
rest[0].call(this, obj, status, jqXHR);
}
]
: [];
// spread the possibly-empty dataObj and newSuccessObj into the new argument list array
const newArgs = [url, ...data, ...newSuccessArr, 'text'];
const prom = jQuery.get.apply(this, newArgs);
const nativeThen = prom.then;
prom.then = function(resolve) {
nativeThen.call(this)
.then((res) => {
const obj = dequoteDigits(this.responseText);
resolve(obj);
});
};
return prom;
};
jQuery.getJSON('https://jsonplaceholder.typicode.com/users', (obj) => {
console.log(obj[0].address.geo);
});
(async () => {
const obj = await jQuery.getJSON('https://jsonplaceholder.typicode.com/users');
console.log(obj[0].address.geo);
// console.log(obj);
})();
<script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script>