admin管理员组

文章数量:1332383

$('.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;
    }
  });
}

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

$('.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;
    }
  });
}

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

Share edited Aug 15, 2010 at 1:48 Gert Grenander 17.1k6 gold badges41 silver badges43 bronze badges asked Aug 15, 2010 at 1:02 ChrisChris 6,1865 gold badges25 silver badges30 bronze badges 1
  • What exactly are you trying to do? – Jamie Wong Commented Aug 15, 2010 at 1:05
Add a ment  | 

1 Answer 1

Reset to default 10

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;
}

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

本文标签: javascriptjQuery Ajax returns undefinedStack Overflow