首次打开移动应用主页时返回错误
"TypeError: Jqueryxxxxxx is not a function" although it shows the API callback results "jQuery111309512500500950475_1459208158307({"code":1,"msg":"Ok","details":{"data"..." according to Firebug.
我必须打开不同的应用页面,然后返回主页才能查看解析的特色商家。
JS代码
case "page-home":
callAjax('getFeaturedMerchant','');
break;
case "getFeaturedMerchant":
displayFeaturedRestaurant( data.details.data ,'list-featured');
break;
case "getFeaturedMerchant":
createElement('list-featured','');
break;
API PHP 代码
public function actiongetFeaturedMerchant()
{
$DbExt=new DbExt;
$DbExt->qry("SET SQL_BIG_SELECTS=1");
$start=0;
$limit=200;
$and='';
if (isset($this->data['restaurant_name'])){
$and=" AND restaurant_name LIKE '".$this->data['restaurant_name']."%'";
}
$stmt="SELECT a.*,
(
select option_value
from
{{option}}
WHERE
merchant_id=a.merchant_id
and
option_name='merchant_photo'
) as merchant_logo
FROM
{{view_merchant}} a
WHERE is_featured='2'
AND is_ready ='2'
AND status in ('active')
$and
ORDER BY sort_featured ASC
LIMIT $start,$limit
";
if (isset($_GET['debug'])){
dump($stmt);
}
if ($res=$DbExt->rst($stmt)){
$data='';
foreach ($res as $val) {
$data[]=array(
'merchant_id'=>$val['merchant_id'],
'restaurant_name'=>$val['restaurant_name'],
'logo'=>AddonMobileApp::getMerchantLogo($val['merchant_id']),
);
}
$this->details=array(
'data'=>$data
);
$this->code=1;$this->msg="Ok";
$this->output();
} else $this->msg=$this->t("No Featured Restaurant found");
$this->output();
}
我被困住了,很困惑是什么导致了这个错误以及如何解决它。
编辑:添加了完整的 callAjax 函数
function callAjax(action,params)
{
/*add language use parameters*/
params+="&lang_id="+getStorage("default_lang");
dump(ajax_url+"/"+action+"?"+params);
ajax_request = $.ajax({
url: ajax_url+"/"+action,
data: params,
type: 'post',
async: false,
dataType: 'jsonp',
timeout: 6000,
crossDomain: true,
beforeSend: function() {
if(ajax_request != null) {
/*abort ajax*/
hideAllModal();
ajax_request.abort();
} else {
},
complete: function(data) {
ajax_request=null;
hideAllModal();
},
success: function (data) {
dump(data);
if (data.code==1){
switch (action)
{
case "getFeaturedMerchant":
displayFeaturedRestaurant( data.details.data ,'list-featured');
//$(".result-msg").text(data.details.total+" Restaurant found");
$(".result-msg").text(data.details.total+" "+ getTrans("Featured Restaurants found",'restaurant_found') );
break
)
else {
/*failed condition*/
switch(action)
{
case "getFeaturedMerchant":
createElement('list-featured','');
//$(".result-msg").text(data.msg);
break;
}
},
error: function (request,error) {
hideAllModal();
if ( action=="getLanguageSettings" || action=="registerMobile"){
} else {
onsenAlert( getTrans("Network error has occurred please try again!",'network_error') );
}
}
}};
调用网址为:
最佳答案
这实际上是 jQuery 在使用 JSONP 时处理 abort 方法的方式的问题,我之前也遇到过。
基本上,JSONP 的工作原理是向 DOM 添加脚本标记,并添加执行时将触发的回调。
与 AJAX 不同,script
标记生成的请求无法取消,因此当您像下面这样调用 abort
时,它只能起作用。
ajax_request.abort();
jQuery 将取消设置它注册的全局回调,jQuery111309512500500950475_1459208158307
在您的情况下,但它无法阻止脚本在加载时尝试运行它。因此,当它尝试调用现在未定义的函数时,您会收到错误。
就个人而言,我认为 jQuery 应该将这些全局处理程序设置为空函数或其他内容,或者有一个选项将其设置为空函数或其他内容,但事实并非如此。对于您的情况,如果可能的话,如果您只想在发送之前中止请求,我建议您避免发出请求。
关于javascript - 类型错误 : jQueryxxxxxx is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36274120/