包装 $.ajax 问题
Wrapped $.ajax issue
$.ajax 被包装到新的 'get' 函数中。
如果js文件中只有一个'get'调用就可以了。
但是连续 2 次调用失败。
更精确,
第一次调用失败 "Uncaught ReferenceError: process is not defined",
第二个是成功的,但是在成功函数中它有第一个 'get' 调用的数据。
正如我所猜测的,'this'/context 存在一些问题。你能给我解释一下吗?
(function() {
"use strict";
function get(url, success, error) {
$.ajax({
type: "GET",
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'process',
url: url,
success: success,
error: error
});
}
get('XXX',
function(data, textStatus, jqXHR) {
console.log("SUCCESS PING 1");
console.log(data);
},
function(jqXHR, textStatus, errorThrown) {
console.log("ERROR PIND 1");
});
get('YYY',
function(data, textStatus, jqXHR) {
console.log("SUCCESS PING 2");
console.log(data);
},
function(jqXHR, textStatus, errorThrown) {
console.log("ERROR PING 2");
});
})();
/*
===========================================
===============console=====================
===========================================
1. ERROR PIND WAR
2. Uncaught ReferenceError: process is not defined
at db?callback=process&_=1485184752755:1
3. SUCCESS PING DB
4. Object {data for first call here}
*/
首先,最好不要指定自定义回调名称('jsonpCallback'参数)
http://api.jquery.com/jQuery.ajax/
jsonpCallback Type: String or Function() Specify the callback function
name for a JSONP request. This value will be used instead of the
random name automatically generated by jQuery. It is preferable to let
jQuery generate a unique name as it'll make it easier to manage the
requests and provide callbacks and error handling. You may want to
specify the callback when you want to enable better browser caching of
GET requests. As of jQuery 1.5, you can also use a function for this
setting, in which case the value of jsonpCallback is set to the return
value of that function.
问题是,jQuery 在具有指定名称的 window 对象中创建全局函数,然后将其删除。
我没有设法全面了解 jQuery 库中发生的事情,但是
这个问题肯定是因为它试图调用刚刚被删除的函数。
删除 jsonpCallback 参数解决了一个问题
$.ajax 被包装到新的 'get' 函数中。
如果js文件中只有一个'get'调用就可以了。 但是连续 2 次调用失败。
更精确, 第一次调用失败 "Uncaught ReferenceError: process is not defined", 第二个是成功的,但是在成功函数中它有第一个 'get' 调用的数据。
正如我所猜测的,'this'/context 存在一些问题。你能给我解释一下吗?
(function() {
"use strict";
function get(url, success, error) {
$.ajax({
type: "GET",
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'process',
url: url,
success: success,
error: error
});
}
get('XXX',
function(data, textStatus, jqXHR) {
console.log("SUCCESS PING 1");
console.log(data);
},
function(jqXHR, textStatus, errorThrown) {
console.log("ERROR PIND 1");
});
get('YYY',
function(data, textStatus, jqXHR) {
console.log("SUCCESS PING 2");
console.log(data);
},
function(jqXHR, textStatus, errorThrown) {
console.log("ERROR PING 2");
});
})();
/*
===========================================
===============console=====================
===========================================
1. ERROR PIND WAR
2. Uncaught ReferenceError: process is not defined
at db?callback=process&_=1485184752755:1
3. SUCCESS PING DB
4. Object {data for first call here}
*/
首先,最好不要指定自定义回调名称('jsonpCallback'参数)
http://api.jquery.com/jQuery.ajax/
jsonpCallback Type: String or Function() Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.
问题是,jQuery 在具有指定名称的 window 对象中创建全局函数,然后将其删除。 我没有设法全面了解 jQuery 库中发生的事情,但是 这个问题肯定是因为它试图调用刚刚被删除的函数。
删除 jsonpCallback 参数解决了一个问题