This question already has answers here:
How do I return the response from an asynchronous call?
                            
                                (39个答案)
                            
                    
                5年前关闭。
        

    

具有这样的代码结构:

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页面readValue上的Button(SomeBuuttonClickEvent)时,将返回空值。如何设置_designTemplateData数据。
..

添加了$ .ajax函数。如何通过使用任何解决方案来解决问题

What code to be added

需要在里面写什么代码:

RequestResponse:函数(数据)
     要么
 ajaxCall:function()

最佳答案

请尝试这个。
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
    }
}();


在ViewPage中:

<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]");
        }

07-27 20:06