【发布时间】:2014-11-11 06:43:35
【问题描述】:
我对 ajax 和 jsonp 真的很陌生,并且在调用文件时遇到了问题。该代码有效。但是每次我在同一个脚本中再次调用同一个函数时,它都会显示“未捕获的 TypeError:未定义不是一个函数”。如果该功能只工作一次,它不应该总是工作吗?
这是我的代码示例
var resultAmount = 0;
start = function(teamFile, rowsInDB, ratio_Over_rows, opplastx_gp, callfunction){
//ajax ONLY calls don't return anything
(function($) {
//Connects to the json teamFile
var url = 'http://xxx.co.uk/football/'+teamFile+'.json?callback=?';
//Automatic refresh
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(data) {
if(callfunction == 'mep'){
resultCount(data, rWin, count);
resultCount(data, rDraw, count);
resultCount(data, rLose, count);
//the total of w/d/l
resultAmount = total[rWin] + total[rDraw] + total[rLose] ;
}else{}
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
}
//Adds the results w, d, l up
resultCount = function(for_data, result, count_r){
count_r = 0;
//Goes through the data
for(k in for_data){
//if equals w, d, 1
if(for_data[k].Results == result){
//Add 1
count_r++;
}else{
}
}
}
//Then I call the function start twice only one works
console.log(start('ast', 7,5,5, 'mep'));
console.log(start('ars', 7,5,5, 'mep'));
只有第一个函数运行而不是第二个它说“未捕获的类型错误:未定义不是函数”。当我围绕第一个函数运行更改它们时,第二个说“未捕获的类型错误:未定义不是函数”。
如果它有助于我的文件看起来像这样
jsonCallback([{"Brad Guzan":"yes","Jed Steer":"no","Ashley Westwood":"yes","Fabian Delph":"no","Ron Vlaar":"yes","Andreas Weimann":"yes","Gabriel Agbonlahor":"no","Nathan Baker":"yes","Leandro Bacuna":"yes","Karim El Ahmadi":"no","Christian Benteke":"no","Ciaran Clark":"no","Matthew Lowton":"yes","Ryan Bertrand":"yes","Antonio Luna":"no","Marc Albrighton":"yes","Libor Koz\u00e1k":"no","Aleksandar Tonev":"no","Yacouba Sylla":"no","Grant Holt":"yes","Joseph Bennett":"yes","Chris Herd":"no","Jordan Bowery":"no","Jores Okore":"no","Gary Gardner":"no","Daniel Johnson":"no","Nicklas Helenius":"no","Jack Grealish":"no","Janoi Donacien":"no","Callum Robinson":"no","last_gp":"lose","2nd_gp":"lose","3rd_gp":"win","4th_gp":"lose","5th_gp":"lose","Home":"home","Results":"lose"});
【问题讨论】:
-
你的意思是错误在线
console.log(start('ars', 7,5,5, 'mep'));?或者堆栈跟踪还有更多内容吗? -
这就是你的全部代码吗?因为如果是的话,你的第二个函数是不完整的,不会编译。因此它不会运行。
-
您在返回的数据中还缺少一个右数组括号。
-
是的,它是 console.log(start('ars', 7,5,5, 'mep'));完整的代码有右括号,它只是很多代码,我不得不缩短它以举例说明正在发生的事情
-
搜索所有代码,确保在其他地方没有
start =,因为这会重新定义您的函数定义。
标签: javascript jquery ajax function jsonp