从数据库到html的AJAX值被跳过

时间:2018-01-14 22:44:59

标签: javascript jquery ajax

如果您点击员工框,我会显示这些员工信息。但有时某些字段的值即使有值也会返回null,但是当我重试它时它会返回ok。这是一些代码问题吗?这是我的代码......

首先,我将元素存储到对象

var uamnumber = $(this).find(".box-list-agents-uamnumber").text();
var agentInfo = $(this).find(".box-list-agents-info").text().split("/");

var agentElement = {
        txtUam: $("#search-txt-uam-number"),
        txtFirstName: $("#search-txt-first-name"),
        txtMiddleName: $("#search-txt-middle-name"),
        txtLastName: $("#search-txt-last-name"),
        txtContactNumber: $("#search-txt-contact-number"),
        txtEmailAddress: $("#search-txt-email-address"),
        txtClassification: $("#search-txt-classification"),
        txtAgentStatus: $("#search-txt-agent-status"),
        txtReasonResignation: $("#search-txt-reason-resignation"),

        txtCsp: $("#search-txt-csp-name"),
        txtProgramId: $("#search-txt-program-name"),
        txtSite: $("#search-txt-site-name"),
        txtBirthDate: $("#search-txt-birth-date"),
        txtLiveDate: $("#search-txt-live-date"),
        txtEndDate: $("#search-txt-end-date"),

        txtProgram: $("#search-program-name")

    };
    var agentParam = {
        uam: uamnumber,
        csp: agentInfo[0],
        program: agentInfo[1]
    }
Dashboard_GetAgentInfo(agentParam, agentElement);

    $("#search-well-tool-access").hide();
    $("#search-well-agent-info").fadeIn();

这是已被调用的函数。

function Dashboard_GetAgentInfo(agentInfo,agentElement) {
$.ajax({

    type: "POST",
    url: "IROA_StoredProcedures.asmx/Dashboard_GetAgentInfo",
    data: JSON.stringify(agentInfo),
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success: function (response) {

        var uamdetails = response.d;
        var appendItem = "";

        $.each(uamdetails, function (index, Dashboard_GetAgentInfoInfo) {

            var uamnumber = Dashboard_GetAgentInfoInfo.uamnumber;
            var firstname = Dashboard_GetAgentInfoInfo.firstname;
            var middlename = Dashboard_GetAgentInfoInfo.middlename;
            var lastname = Dashboard_GetAgentInfoInfo.lastname;
            var contactnumber = Dashboard_GetAgentInfoInfo.contactnumber;
            var emailaddress = Dashboard_GetAgentInfoInfo.emailaddress;
            var csp = Dashboard_GetAgentInfoInfo.csp;
            var cspid = Dashboard_GetAgentInfoInfo.cspid;
            var program = Dashboard_GetAgentInfoInfo.program;
            var programid = Dashboard_GetAgentInfoInfo.programid;
            var site = Dashboard_GetAgentInfoInfo.site;
            var siteid = Dashboard_GetAgentInfoInfo.siteid;
            var birthdate = Dashboard_GetAgentInfoInfo.birthdate;
            var livedate = Dashboard_GetAgentInfoInfo.livedate;
            var enddate = Dashboard_GetAgentInfoInfo.enddate;
            var classification = Dashboard_GetAgentInfoInfo.classification;
            var agentStatus = Dashboard_GetAgentInfoInfo.agentstatus;
            var reasonResignation = Dashboard_GetAgentInfoInfo.reasonresignation;

            $(agentElement.txtUam).val(uamnumber);
            $(agentElement.txtFirstName).val(firstname);
            $(agentElement.txtMiddleName).val(middlename);
            $(agentElement.txtLastName).val(lastname);
            $(agentElement.txtContactNumber).val(contactnumber);
            $(agentElement.txtEmailAddress).val(emailaddress);
            $(agentElement.txtClassification).val(classification);
            $(agentElement.txtAgentStatus).val(agentStatus);
            $(agentElement.txtReasonResignation).val(reasonResignation);

            $(agentElement.txtCsp).val(cspid)
            $(agentElement.txtProgramId).val(programid);
            $(agentElement.txtSite).val(siteid);

            $(agentElement.txtBirthDate).val(birthdate);
            $(agentElement.txtLiveDate).val(livedate);
            $(agentElement.txtEndDate).val(enddate);


            $(agentElement.txtProgram).text(program);

            NumbersOnly();

        });

    },

    error: function (XMLhttpRequest) {
        alert("error in Dashboard_GetAgentInfo");
        console.log(XMLhttpRequest);
    }

});
}

这是一个名为

的Web服务
public List<Dashboard_GetAgentInfoDetails> Dashboard_GetAgentInfo(string uam, int csp, int program) /*int CSP, int Program*/
    {
        DataTable table = null;
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "[Dashboard_GetAgentInfo]";
        cmd.Parameters.AddWithValue("@uam", uam);
        cmd.Parameters.AddWithValue("@csp", csp);
        cmd.Parameters.AddWithValue("@program", program);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        table = this.dbConn.ExecuteDataTable(cmd);

        Dashboard_GetAgentInfo_Details.Clear();

        foreach (DataRow row in table.Rows)
        {
            Dashboard_GetAgentInfoDetails _list = new Dashboard_GetAgentInfoDetails();

            _list.uamnumber = row["UAM #"].ToString();
            _list.firstname = row["First Name"].ToString();
            _list.middlename = row["Middle Name"].ToString();
            _list.lastname = row["Last Name"].ToString();
            _list.contactnumber = row["Contact Number"].ToString();
            _list.emailaddress = row["Email Address"].ToString();
            _list.csp = row["CSP"].ToString();
            _list.cspid = Convert.ToInt32(row["CSPID"].ToString());
            _list.program = row["Program"].ToString();
            _list.programid = Convert.ToInt32(row["ProgramID"].ToString());
            _list.site = row["Site"].ToString();
            _list.siteid = Convert.ToInt32(row["SiteID"].ToString());
            _list.birthdate = row["BirthDate"].ToString();
            _list.livedate = row["LiveDate"].ToString();
            _list.enddate = row["EndDate"].ToString();
            _list.classification = Convert.ToInt32(row["Classification"].ToString());
            _list.agentstatus = row["Agent Status"].ToString();
            _list.reasonresignation = row["Reason Resignation"].ToString();

            Dashboard_GetAgentInfo_Details.Add(_list);
        }

        return Dashboard_GetAgentInfo_Details;
    }

将元素存储到对象中并将其作为参数传递是一种编码的好习惯吗?什么可能是select没有价值的原因,即使我尝试console.log该值并且它返回正常?

2 个答案:

答案 0 :(得分:1)

我认为问题在于:

$(agentElement.txtUam).val(uamnumber);
$(agentElement.txtFirstName).val(firstname);
...

你应该这样做:

agentElement.txtUam.val(uamnumber);
agentElement.txtFirstName.val(firstname);
...

不需要使用jquery选择器$,因为agentElement.txtUam已经是一个,在对象内部收集元素是最佳做法,因为您不能将每个元素作为参数传递

答案 1 :(得分:0)

对此的完美答案是添加回调功能,以便在添加option之前首先使用valfunction Filtering_GetSite(siteElement, callback) { if (typeof (callBack) == "function") { callBack(); } } 。以下是添加回调函数的想法

callback

Filtering_GetSite(sample)参数的行检查是为了确保它在执行之前是一个函数,这样你就可以在省略回调函数时调用像Filtering_GetSite(sample,function(){}) View这样的函数< / p>