jQuery Ajax返回未定义 [英] jQuery Ajax returns undefined

查看:96
本文介绍了jQuery Ajax返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$('.updateDescriptionProduct').click( function() {
  if ( updateProductDescription(productID, description) == 'true') {
    alert('success!');
  } else {
    alert('did not update');        
  }
});

function updateProductDescription(productID, description) {
  $.ajax({
    url: '/index.php/updateProductDescription',
    global: false,
    type: 'POST',
    data: ({productID: productID, description: description}),
    dataType: 'html',
    async:false,
    success: function(msg){
      alert(msg); 

      return msg;
    }
  });
}

该函数本身说的是true,但是我的click事件以undefined的形式返回.

The function itself says true, but my click event comes back as undefined.

推荐答案

return适用于回调.尝试在初始函数中设置变量,然后在回调中设置值,然后返回它吗?

the return applies to the callback. Try setting a variable in the initial function and setting the value in the callback, then returning it?

function updateProductDescription(productID, description) {
    var ret = false;
    $.ajax({
          url: '/index.php/updateProductDescription',
          global: false,
          type: 'POST',
          data: ({productID: productID, description: description}),
          dataType: 'html',
          async:false,
          success: function(msg){
            ret = msg;
          }
    });
    return ret;
}

我从未进行过async通话,但似乎应该可以.让我知道是否可以.

I haven't ever done an async call but it seems this should work. Let me know if it doesn't.

这篇关于jQuery Ajax返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆