如何从javascript函数返回一个值

时间:2014-01-16 08:07:27

标签: javascript asmx

我有以下函数,我希望返回htm值但是我得到一个未定义的。我得到一个返回值,因为我已经检查过了。

 function loadData(uid) {



            $.ajax({
                type: "POST",
                url: '<%= page.resolveURL("~")%>Haggler.asmx/getLovedProductsSellerStoreByFbId',
                //url: '<%= Page.ResolveUrl("~")%>Haggler.asmx/GetFacebookFriends',
                data: '{FacebookId:' + uid + ',pageIndex:' + JSON.stringify(pageIndex) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                async: true,

                // Page parameter to make sure we load new data
                success: function (data) {
                    var myObject = eval('(' + data.d + ')');

                    //alert('getProductDescription' + JSON.stringify(myObject));
                    var html = '';
                    pageIndex++;
                    var htmlCategoryList = '';
                    var i = 0, length = myObject.length;
                    var _productLink = '';
                    var _productFullLink = '';
                    if (length > 0) {
                        pageCount = myObject[0].PageCount;

                        if (length > 0) {

                            for (; i < length; i++) {

                                if (myObject[i].ShippingQuantity > 0) {

                                    _productLink = myObject[i].SellReqID + '/product/' + myObject[i].CurrentNodeName;
                                    _productFullLink = "http://www.xarato.com/" + myObject[i].SellReqID + "/product/" + myObject[i].CurrentNodeName;
                                    if (myObject[i].Discount == 0) {
                                        /**
                                        if (parts[parts.length-1] == 'loves') {
                                            html += '<li class="polaroid"><div class="prodoptionbg prodseller"><span>Listed by <a href="/' + _storeLink + '">' + myObject[i].FirstName + ' ' + myObject[i].LastName + '</a></span></div><a href="/' + _productLink + '"><div style="position:relative;"><img alt="' + myObject[i].NodeName + '" src="/' + myObject[i].
                                            html += '<li class="polaroid"><a href="/' + _productLink + '"><div style="position:relative;"><img alt="' + myObject[i].RequestTitle + '" src="/' + myObject[i].Image1 + '"_thumb.jpg" width="200" height="' + myObject[i].ThumbHeight1 + '"><div class="options"><span class="favs" id="span' + myObject[i].SellReqID + '">' + myObject[i].Likes + '</span><span class="fav" onclick="calculateLike(event,' + myObject[i].SellReqID + ')">like it!</span></div></div><div class="prod"><span>' + myObject[i].RequestTitle + '</span></div><div class="prodprice1"><span style="font-weight:700;">Rs. ' + Math.round(parseFloat(myObject[i].MRPrice) + parseFloat(myObject[i].ShippingPrice)) + '</span></div></a></li>';

                                        }else{ **/
                                        //alt="' + myObject[i].RequestTitle + '"
                                        html += '<img alt="' + myObject[i].RequestTitle + '" src="/' + myObject[i].Image1 + '_thumb.jpg" width="200" height="' + myObject[i].ThumbHeight1 + '">';
                                        //}

                                    }
                                    else {
                                        /**if (parts[parts.length-1] == 'loves') {
                                            var _finalPrice = parseFloat(myObject[i].MRPrice) - (parseFloat(myObject[i].Discount) * parseFloat(myObject[i].MRPrice))/100

                                            html += '<li class="polaroid"><div class="prodoptionbg prodseller"><span>Listed by <a href="/' + _storeLink + '">' + myObject[i].FirstName + ' ' + myObject[i].LastName + '</a></span></div><a href="/' + _productLink + '"><div style="position:relative;"><img alt="' + myObject[i].NodeName + '" src="/' + myObject[i].Preview + '_thumb.jpg" width="200" height="' + myObject[i].Height + '"><div class="options"><span class="favs" id="span' + myObject[i].NodeId + '">' + myObject[i].Likes + '</span><span class="fav" onclick="calculateLike(event,' + myObject[i].NodeId + ')">like it!</span></div><div class="kjss"><span>' + myObject[i].Discount + '% Off</span></div></div><div class="prod"><span>' + myObject[i].NodeName + '</span></div><div class="prodprice1"><span style="color:#777777; text-decoration:line-through">Rs. ' + myObject[i].MRPrice + '</span> &nbsp;<span style="font-weight:700;">Rs. ' + Math.round(_finalPrice + parseFloat(myObject[i].ShippingPrice)) + '</span></div></a></li>';
                                        }else{**/
                                        //alt="' + myObject[i].RequestTitle + '"
                                        html += '<img alt="' + myObject[i].RequestTitle + '" src="/' + myObject[i].Image1 + '_thumb.jpg" width="200" height="' + myObject[i].ThumbHeight1 + '">';
                                        //}

                                    }
                                }
                            }


                            if (clearHtml) {
                                //  $('.bxslider').html('');

                                //htm = '<li>"' + html + '"</li>';

                            }

                            // var htmli = '<li>"' + html + '"</li>';
                            // $('.bxslider').append(htmli);

                            htm = '<li>' + html + '</li>';
                            alert(htm);
                            return htm;

                            clearHtml = false;

                            var options = {
                                autoResize: true, // This will auto-update the layout when the browser window is resized.
                                container: $('#main'), // Optional, used for some extra CSS styling
                                offset: 17, // Optional, the distance between grid items
                                itemWidth: 225 // Optional, the width of a grid item 
                            };

                        }
                        else {

                            return;
                        }
                    }
                    else {
                        return;
                    }


                },

                failure: function (data) {
                    alert('failture');
                },
                error: function (data) {
                    alert(data.responseText);

                }


            });


        }

这是我如何获取数据,但我得到一个未定义的值。

 HtmlM += loadData(myObject.data[i].uid);

请帮帮我。

1 个答案:

答案 0 :(得分:0)

将loadData更改为

function loadData(uid, delegate) {

  ///In ajax instead of return use 
  delegate(htm);
}

然后像那样打电话

loadData(myObject.data[i].uid, function(html){ HtmlM += html ;});

Ajax是异步的,所以你不能只返回(是的,你可以同步,但不是正确的方式)