如何从一个Ajax调用抢返回值?返回值、Ajax

2023-09-10 13:18:42 作者:最繁华时最悲凉

我要抢一个Ajax调用使用函数的值。但价值总是返回未定义。返回值是唯一的1或0

这是我的code:

  $(函数(){
   $('#add_product)。点击(函数(){
     变种I = $('#PRODUCT_NAME)VAL()。
     参数='PRODUCT_NAME ='+ I;
     VAR值= check_product(参数);
     警报(值);
     返回false;
   });
});

功能check_product(参数){
  $阿贾克斯({
    键入:POST,
    数据:参数,
    网址:baseURL时+购物车/ check_product_name /',
    成功:函数(结果){
      //警报(结果);
      返回结果;
    }
 });
}
 

大家好,还在努力让这一个工作。我得到的价值现在显示1或0。什么我尝试,现在做到的是我可以在if语句如何。我想有这样的事情。如果VAL = 0,则返回true;否则返回FALSE。林不知道我在正确的轨道上使用AJAX功能。但是,如果有更好的办法,你可以告诉我我将AP preciate吧。

  $(函数(){
       $('#add_product)。点击(函数(){
         变种I = $('#PRODUCT_NAME)VAL()。
         参数='PRODUCT_NAME ='+ I;
         check_product(参数).done(函数(值){
        VAR VAL =价值; //等待,直到阿贾克斯完成
         });
         如果(VAL == 0){
            返回true;
         } 其他{
            返回false;
         }
       });
    });

功能check_product(参数){
  返回$阿贾克斯({
    键入:POST,
    数据:参数,
    网址:baseURL时+购物车/ check_product_name /
 });
}
 

解决方案

这是异步的,所以你必须等待AJAX​​调用来获取数据回来之前,你可以提醒它。你可以做到这一点很容易通过返回Ajax调用,并使用进行(),像这样:

  $(函数(){
    $('#add_product)。点击(函数(){
        变种I = $('#PRODUCT_NAME)。VAL()
            PAR ='PRODUCT_NAME ='+ I;

        check_product(PAR).done(函数(值){
            警报(值); //等待,直到阿贾克斯完成
        });

        返回false;
    });
});

功能check_product(参数){
    返回$阿贾克斯({
        键入:POST,
        数据:参数,
        网址:baseURL时+购物车/ check_product_name /
    });
}
 
ajax 返回值的问题

I wanted to grab the value on an ajax call using a function. but the value always return as undefined. return value is only 1 or 0.

here is my code:

$(function(){ 
   $('#add_product').click(function(){ 
     var i = $('#product_name').val(); 
     param = 'product_name='+i; 
     var value = check_product(param); 
     alert(value); 
     return false; 
   }); 
});

function check_product(param){ 
  $.ajax({ 
    type : 'POST', 
    data : param, 
    url : baseurl+'cart/check_product_name/', 
    success : function(result){ 
      //alert(result); 
      return result; 
    } 
 });
}

Hi guys, still working on getting this one work. I get the value now showing 1 or 0. What im trying to accomplish now is how I can it in the if statement. I wanted to have something like this. If val = 0 return true; else return false. Im not sure I'm in the right track of using the ajax function. But if there a better way you can show me I will appreciate it.

$(function(){ 
       $('#add_product').click(function(){ 
         var i = $('#product_name').val(); 
         param = 'product_name='+i; 
         check_product(param).done(function(value) {
        var val = value; //waits until ajax is completed
         });
         if(val == 0){
            return true;
         } else{
            return false;
         }
       }); 
    });

function check_product(param){ 
  return $.ajax({ 
    type : 'POST', 
    data : param, 
    url : baseurl+'cart/check_product_name/'
 });
}

解决方案

It's asynchronous, so you have to wait for the ajax call to get the data back before you can alert it. You can do that easily by returning the ajax call and using done(), like so:

$(function() {
    $('#add_product').click(function() {
        var i   = $('#product_name').val(),
            par = 'product_name=' + i;

        check_product(par).done(function(value) {
            alert(value); //waits until ajax is completed
        });

        return false;
    });
});

function check_product(param) {
    return $.ajax({
        type : 'POST',
        data : param,
        url  : baseurl + 'cart/check_product_name/'
    });
}​