0

以下是代码

 function CreateDiv(D) {

  D.body = function () {
            var d;

             $.ajax({
                type: "GET",
                url: "Default.aspx",
                data: 'ExtrFlag=GetChat&userID=1&FriendID=' + this.id,
                success: function (data) {

                    d= data.split('@@@')[0]);

                },
                error: function (xhr) {
                   alert(xhr.responseText);

                }
            });
   var reutnData = "<div class='chatBody' id='chatBody" + this.id + "' >"+d+"</div>";
            return reutnData;
        };
       D.create = function () {
            var exist = $("#Container").find("div[id=chatbox" + this.id + "]");
            if (exist.length == 0) {

                   var ToAppend = "<div id='chatbox" + this.id + "' style='" +  D.style() + "' class='chatboxclass' >" + D.header() +D.body()+ "</div>";
                $("#Container").append(ToAppend);
                align++;
            }

        };

        return D;
    }

 function NewChat(id,username,picture) {

        var div = new CreateDiv({ width: 250, height: 285, id: id, username: username, picture: picture });
        div.create();
    }

d总是未定义并且它没有返回 ajax 请求我做错了什么?

4

1 回答 1

0

示例代码:

JS-

var dataString = 'ExtrFlag=GetChat&userID=1&FriendID=' + this.id

$.ajax({
         type: "POST",
         url: "ajax_function/updatefn.asmx/addABC",
         data: "{ 'prefix': '" + dataString + "'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: OnSuccessCall,
         error: OnErrorCall
    });


 function OnSuccessCall(response) {
                var responeValue = response.d;
                var costpart = String(responeValue).split("-");
                var get_id = costpart[1];
.....
}

代码隐藏:

   [WebMethod]
    public string[] addABC(string prefix)
    {
        string status = ""; string strFLag="";
        string val_id = "";
        string datastr = prefix;
        string[] val = datastr.Split('&');
        string[] extrFlag = val[0].Split('=');
        strFLag= extrFlag [1];
        // code logici .i.e  which returns strFLag="abc";  val_id="23";

        d.Add(string.Format("{0}-{1}", strFLag, val_id));
        return d.ToArray();
    }
于 2013-09-20T08:07:07.783 回答