Javascript 对象初始化不工作

Javascript Object Initialization not working

具有这样的代码结构:

SkillEidt.js(Javascript 文件):

                 var SkillEdit = ({

                     _designtemplate: ["", "input", "dropdownlist", "checkbox"],
                    _designTemplateData: {},


                    readValue: function() {
                         /* when try to read value from customer.Html it's null */
                       return this._designTemplateData;;   
                    },

                    RequestResponse: function (data) {
                            /* able to get and set value from ajax call */
                            this._designTemplateData = data;
                        },

                    ajaxCall : function() {
                         $.ajax({
                        url: "/VendorDetails/GetVendorDetails",
                        type: "POST",
                        async: true,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        error: function (result) {
                        alert(result.statusText);
                        },
                        success: function (result) {
                           requestReponse(result);
                       }
                   });
                });

SkillEdit.ajaxCall() /* very important  to set _designTemplateData data  */

Customer.Html(页数):

                <script src="~/Scripts/SkillEdit.js"></script>
                <script type="text/javascript">
                 function SomeBuuttonClickEvent() {
                  var notAbleToGetValue =  SkillEdit.readValue();
                 }
                </script>
    ------------------------------------------------------------------------

调试时看到 SkillEdit.ajaxCall() 将调用 ajaxCall() 方法,成功时将调用 RequestResponse 并设置 _designTemplateData。但是当我在 Customer.Html 页面上单击按钮 (SomeBuuttonClickEvent) 时 readValue 返回空值。我如何设置 _designTemplateData 数据。 ..

已添加 $.ajax 功能。如何使用任何解决方案解决问题

What code to be added

里面需要写什么代码:

请求响应:函数(数据) 或者 ajax调用:函数()

您的代码似乎有效:(运行 如下)

var SkillEdit = {

        _designtemplate: ["", "input", "dropdownlist", "checkbox"],
         _designTemplateData: {},

    readValue: function() {
        return this._designTemplateData;;   
    },

    RequestResponse: function (data) {
        this._designTemplateData = data;
    },

    ajaxCall : function() {
        this.RequestResponse("test successful");
    }
};

SkillEdit.ajaxCall()
    
alert(SkillEdit.readValue()) // Should alert "Test successful"

您的错误可能在于处理您的 ajax 回复。

请试试这个。 SkillEdit.js

var SkillEdit = function(){

    _designtemplate= ["", "input", "dropdownlist", "checkbox"],
    _designTemplateData= {},


    readValue = function () {
        $('#templabel').text(_designTemplateData);
        return _designTemplateData;
    },

    RequestResponse = function (data) {
        debugger;
        return _designTemplateData = data;
    },

    ajaxCall= function () {
        //return this.RequestResponse(["12", "13"]);
        $.ajax({
            url: "/Home/GetVendorDetails",
            type: "POST",
            async: true,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (result) {
                alert(result.statusText);
            },
            success: function (result) {
                RequestResponse(result);
                console.log(result);
            }
        });
    }

    return {
        readValue: readValue,
        RequestResponse: RequestResponse,
        ajaxCall : ajaxCall
    }
}();

在查看页面中:

<script src="~/js/SkillEdit.js"></script>
<label id="templabel"></label>
<input type="button" value="stake overflow question" onclick="return SkillEdit.ajaxCall();"/>
<input type="button" value="read Value answer" onclick="return SkillEdit.readValue();" />

在控制器中:

 [HttpPost]
        public ActionResult GetVendorDetails()
        {
            return Json("[12,13]");
        }